CodeIgniter server based 404 - codeigniter

I've installed CodeIgniter on my localhost xampp server in the directory:
localhost/CI/
When I visit that directory directly I get to see the homepage but when I try to vist any other page I get a server based 404 page. I don't get the see the CI 404.
I already tried playing around with the uri_protocol but I can't get it to work. Any clue?
routes.php
$route['page/create'] = 'page/create';
$route['(:any)'] = 'page/view/$1';
$route['default_controller'] = 'page/view/hello-world';
$route['404_override'] = '';
Page controller
class Page extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('page_model');
}
public function view($slug)
{
$data['page'] = $this->page_model->get_page($slug);
if (empty($data['page']))
{
show_404();
}
$data['title'] = $data['page']['title'];
$this->load->view('templates/header', $data);
$this->load->view('page/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a new page';
$this->form_validation->set_rules('title', 'Title', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('page/create');
$this->load->view('templates/footer');
}
else
{
$this->page_model->set_page();
$this->load->view('page/success');
}
}
}
The "Hello world!" shows nicely, but I can't get the create page to work. The view is located in views/page/create.php

xampp !!!! is the problem the mode rewite is not working fine wid it .. install apache as a standalone , add php and mysql and you can work fine any way it will cause other erros
$route['default_controller'] = 'page/view/hello-world';
change that to
$route['default_controller'] = 'page';
then move it to the top so you have
$route['default_controller'] = 'page';
$route['404_override'] = '';
$route['page/create'] = 'page/create';
$route['(:any)'] = 'pages/view/$1';
the order of things is inportant inside the routes.php
and inside page.php controler add a function index()

Related

Try to use the codeigniter's file upload library as a general function from Helpers

Can anybody help as I am trying to use the codeigniter's upload library from the helpers folder but I keep getting the same error that I am not selecting an image to upload? Has any body tried this before?
class FileUpload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'file_uploading'));
$this->load->library('form_validation', 'upload');
}
public function index() {
$data = array('title' => 'File Upload');
$this->load->view('fileupload', $data);
}
public function doUpload() {
$submit = $this->input->post('submit');
if ( ! isset($submit)) {
echo "Form not submitted correctly";
} else { // Call the helper
if (isset($_FILES['image']['name'])) {
$result = doUpload($_FILES['image']);
if ($result) {
var_dump($result);
} else {
var_dump($result);
}
}
}
}
}
The Helper Function
<?php
function doUpload($param) {
$CI = &get_instance();
$CI->load->library('upload');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|png|jpg|jpeg|png';
$config['file_name'] = date('YmdHms' . '_' . rand(1, 999999));
$CI->upload->initialize($config);
if ($CI->upload->do_upload($param['name'])) {
$uploaded = $CI->upload->data();
return $uploaded;
} else {
$uploaded = array('error' => $CI->upload->display_errors());
return $uploaded;
}
}
There are some minor mistakes in your code, please fix it as below,
$result = doUpload($_FILES['image']);
here you should pass the form field name, as per your code image is the name of file input.
so your code should be like
$result = doUpload('image');
then, inside the function doUpload you should update the code
from
$CI->upload->do_upload($param['name'])
to
$CI->upload->do_upload($param)
because Name of the form field should be pass to the do_upload function to make successful file upload.
NOTE
Make sure you added the enctype="multipart/form-data" in the form
element

How to fix session being auto generated when page is reloaded/refresh using Codeigniter and Database as session driver?

I have my little project running perfectly on my local computer. However, when I run it into my laptop, an entry is automatically loaded in my ci_sessions table each time the page is being reloaded or refresh. I am using the database as my session driver.
Based on the screenshot: row 4 says that my login session store successfully. However, the 2 extra rows (5, 6) that are being added cause this code to fail:
public function isLoggedIn()
{
if($this->session->userdata('logged_in') === true) {
redirect('home', 'refresh');
}
}
public function isNotLoggedIn()
{
if($this->session->userdata('logged_in') !== true) {
redirect('login', 'refresh');
}
}
here's my config.php
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
$config['cookie_prefix'] = '';
$config['cookie_domain'] = '';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;
Here's my Page Controller
<?php
class Pages extends MY_Controller
{
public function view($page = 'login')
{
if (!file_exists(APPPATH.'views/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
if($page == 'login') {
$this->isLoggedIn();
$this->load->view($page, $data);
}
else{
$this->isNotLoggedIn();
$this->load->view($page, $data);
}
}
}
MY_Controller Class
<?php
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
public function isLoggedIn()
{
if($this->session->userdata('logged_in') === true) {
redirect('home', 'refresh');
}
}
public function isNotLoggedIn()
{
if($this->session->userdata('logged_in') !== true) {
redirect('login', 'refresh');
}
}
}
So far I have already tried adding the code below to my autoload.php but no luck.
$autoload['libraries'] = array('database', 'session');
Note: again this works in another unit with a similar setup.
after trying some work around, Codeigniter 3 is not yet compatible with php 7. I have to downgrade my php version to 5.6 to make it work. thanks folks for helping.
I suffered a lot of troubles with the original CI session library (included what you mention). Finally I arrived to this replacement that use native PHP session. It works!
Believe me, in the middle of a project, I did not stop to wonder why. Just works.
Here it is: CodeIgniter Native Session
BUT, due that it is an old library you MUST made some hacks. You can check those simple hacks in the library's forum
Just drop this file in codeigniter's library directory.

codeigniter redirecting to a url

I have a website with a url like so (which works fine)
http://www.page.com/en/controller
But there always have to be a language and a controller in the url otherwise the page doesn't load or there is no language (no text).
Is it possible that when I enter a url like this
http://www.page.com
I get redirected to
http://www.page.com/en/controller
And the controller would be hidden? Only this would be left (my links require first segment to load a page with a particular language)
http://www.page.com/en
p.s tried routing and redirect, but with no luck
Sorry for the late response, I was away.
my routes
$route['default_controller'] = 'arena/display';
$route['(:any)/renginiai'] = "renginiai/getevents";
$route['(:any)/arena'] = "arena/display";
$route['(:any)/paslaugos'] = "paslaugos/displayServices";
$route['(:any)/kontaktai'] = "kontaktai/displayContacts";
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
My_Controller which is located in core folder. I load my language libraries from here.
/**
*
*/
class MY_Controller extends CI_Controller{
public function __construct()
{
parent::__construct();
$languages = array("lt", "en");
if(in_array($this->uri->segment(1), $languages)){
$this->lang->load($this->uri->segment(1), $this->uri->segment(1));
}
}
}
And this is my front page controller
<?php
class Arena extends MY_Controller{
public function display($year = NULL, $month = NULL){
/*$this->load->model('Mycal_model');*/
$this->load->model('Image_model');
$data['images'] = $this->Image_model->get_image_data();
$this->load->view('includes/head');
$this->load->view('includes/nav', $data);
$this->load->view('includes/header', $data);
//$this->load->view('includes/calendar', $data);
$this->load->view('includes/section');
$this->load->view('includes/footer');
}
}
open application/config/routes.php file and change
$route['default_controller'] = "en/controller";

how to use jump to a specific div of current page in codeigniter

How do i convert this to codeigniter?
my php code for calling link is this
<ul>
<li>Register Here
<li>Login
</ul>
When I call this this specific link will be called.
<?php
#$opt = $_GET['option'];
if($opt=="") {
include('register.php');
error_reporting(1);
} else {
switch($opt) {
case 'register':
include('register.php');
break;
case 'login':
include('login.php');
break;
}
}
But I don't know how to do it in code igniter.
please help me
Try using a controller like Welcome.php or something
<?php
class Welcome extends CI_Controller {
public function index() {
// Load other data stuff.
// You should autoload the url helper.
$this->load->helper('url');
$opt = $this->input->get('option');
if($opt == "") {
$data['title'] = 'Register';
$this->load->view('header', $data);
$this->load->view('register');
$this->load->view('footer');
error_reporting(1);
} else {
switch($opt) {
case 'register':
$data['title'] = 'Register';
$this->load->view('header', $data);
$this->load->view('register');
$this->load->view('footer');
break;
case 'login':
$data['title'] = 'Login';
$this->load->view('header', $data);
$this->load->view('login');
$this->load->view('footer');
break;
}
}
}
Config.php
$config['base_url'] = 'http://localhost/yourproject/';
$config['index_page'] = 'index.php';
URI
$config['uri_protocol'] = 'REQUEST_URI';
To this
$config['uri_protocol'] = 'QUERY_STRING';
And then you can enable
$config['enable_query_strings'] = TRUE;
And use the built in codeigniter query strings.
Then you would use a link like
<li><?php echo anchor('c=welcome&option=register', 'Register');?></li>
The letter c mean controller in codeigniter query string but you can change that in config.php

How to Codeigniter HMVC Callback function error on live server?

AGood morning all.
I have build a website in codeigniter3 hmvc. all routes and callback function work perfectly on my localhost xampp and after uploading the project on godday share hosting live server I am facing callback funciton problems and below errors show in log file I don't understand why this happen. my user logging function also show this error:-
Unable to access an error message corresponding to your field name Password.(verifylogin)
Error Logs in logs folder of codeigniter
ERROR - 2015-09-16 01:30:08 --> 404 Page Not Found:
ERROR - 2015-09-16 01:30:29 --> 404 Page Not Found: /index
ERROR - 2015-09-16 01:30:30 --> 404 Page Not Found:
ERROR - 2015-09-16 01:31:30 --> Could not find the language line "form_validation_verifylogin"
ERROR - 2015-09-16 01:31:30 --> 404 Page Not Found: /index
These are my application/config/config.php
$config['index_page'] = "index.php?";
$config['uri_protocol'] = "QUERY_STRING";
$config['url_suffix'] = '';
$config['language'] = 'english';
Bellow is my application/config/routes.php setting
$route['Frontend/album_photos/(:num)/(:any)/(:num)'] = 'Frontend/album_photos/index/$1/$2/$3';
$route['Frontend/album_photos/(:num)/(:any)'] = 'Frontend/album_photos/index/$1/$2';
$route['Frontend/news_detail/(:num)/(:any)'] = 'Frontend/news_detail/index/$1/$2';
$route['(:any)/(:any)'] = 'Frontend/index/$1';
$route['(:any)'] = 'Frontend/index/$i';
$route['default_controller'] = 'Frontend/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['Frontend/download_documents'] = 'Frontend/download_documents/$1';
//these routes works for admin panel
$route['Users/index'] = 'Users/index';
$route['Users/validate_login'] = 'Users/validate_login';
$route['Users/logout'] = 'Users/logout';
$route['Dashboard/index'] ='Dashboard/index';
$route['Aps_pages/manage'] ='Aps_pages/manage';
$route['Aps_pages/create'] ='Aps_pages/create';
$route['Aps_pages/submit'] ='Aps_pages/submit';
$route['Aps_pages/delete'] ='Aps_pages/delete';
$route['Aps_news/manage'] ='Aps_news/manage';
$route['Aps_news/create'] ='Aps_news/create';
$route['Aps_news/submit'] ='Aps_news/submit';
$route['Aps_news/delete'] ='Aps_news/delete';
$route['Aps_events/manage'] ='Aps_events/manage';
$route['Aps_events/create'] ='Aps_events/create';
$route['Aps_events/submit'] ='Aps_events/submit';
$route['Aps_events/delete'] ='Aps_events/delete';
$route['Document_upload/manage'] ='Document_upload/manage';
$route['Document_upload/create'] ='Document_upload/create';
$route['Document_upload/submit'] ='Document_upload/submit';
$route['Document_upload/delete'] ='Document_upload/delete';
$route['Aps_image_slider/manage'] ='Aps_image_slider/manage';
$route['Aps_image_slider/create'] ='Aps_image_slider/create';
$route['Aps_image_slider/submit'] ='Aps_image_slider/submit';
$route['Aps_image_slider/delete'] ='Aps_image_slider/delete';
$route['Aps_testimonials/manage'] ='Aps_testimonials/manage';
$route['Aps_testimonials/create'] ='Aps_testimonials/create';
$route['Aps_testimonials/submit'] ='Aps_testimonials/submit';
$route['Aps_testimonials/delete'] ='Aps_testimonials/delete';
$route['Aps_album_photo/manage'] ='Aps_album_photo/manage';
$route['Aps_album_photo/create'] ='Aps_album_photo/create';
$route['Aps_album_photo/submit'] ='Aps_album_photo/submit';
$route['Aps_album_photo/delete'] ='Aps_album_photo/delete';
$route['Aps_album_photo/search_result'] ='Aps_album_photo/search_result';
$route['Aps_photo_album/manage'] ='Aps_photo_album/manage';
$route['Aps_photo_album/create'] ='Aps_photo_album/create';
$route['Aps_photo_album/submit'] ='Aps_photo_album/submit';
$route['Aps_photo_album/delete'] ='Aps_photo_album/delete';
This is my Home.php Controller in Frontend Modules
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends MX_Controller
{
public $data =array();
function __construct()
{
parent::__construct();
$this->load->model('mdl_home');
$this->load->helper('site_helper');
$this->load->library('pagination');
}
public function index()
{
$slug = (string)$this->uri->segment(1);
//die($slug.'=is a slug');
$this->data['page'] = $this->mdl_home->get_pages(trim($slug));
// Fetch the page data
if(!empty($this->data['page'])){
$method = '_' .trim($this->data['page']->ViewTemplate);
if (method_exists($this, $method)) {
$this->$method();
}
else {
//die('nothing found method not found');
log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
show_error('Could not load template ' . $method);
}
//some config to view out put
$this->data['view_file'] = $this->data['page']->ViewTemplate;
$this->data['module_name'] ='Frontend';
//these variable used for main page layout meta keywords description title
$this->data['page_title'] = $this->data['page']->Title;
$this->data['keywords'] = $this->data['page']->Keywords;
$this->data['description'] = $this->data['page']->Description;
$this->data['body'] = $this->data['page']->Body;
//load template module for front end
$this->load->module('Template');
$this->template->frontend_layout($this->data);
}else{
show_404();
}
}
//home page method
private function _home_page(){
$this->data['SliderList'] = $this->mdl_home->get_slider_list();
$this->data['NewsList'] = $this->mdl_home->get_news_with_limit('aps_news',12,'','','NewsDate DESC');
$this->data['EventsList'] = $this->mdl_home->get_event_list();
$this->data['TestimonialList'] = $this->mdl_home->get_testimonial_list();
$this->data['Documents'] = $this->mdl_home->get_uploaded_documents();
}
//this method is for static pages
private function _static_page(){
}
//this method load news page
private function _news_page(){
$table = 'aps_news';
$offset = ($this->uri->segment(2) != '' ? $this->uri->segment(2): 0);
$config['total_rows'] = $this->mdl_home->total_rows($table);
$config['per_page']= 20;
$config['uri_segment'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = floor($choice);
$config['base_url']= site_url((string)$this->uri->segment(1));
$this->pagination->initialize($config);
$this->data['paginglinks'] = $this->pagination->create_links();
$this->data['NewsList'] = $this->mdl_home->get_news_with_limit($table,$config["per_page"], $offset,'NewsDate DESC');
}
//this method load contact page
private function _contact_page(){
}
//this method load events page
private function _events_page(){
$table = 'aps_events';
$offset = ($this->uri->segment(2) != '' ? $this->uri->segment(2): 0);
$config['total_rows'] = $this->mdl_home->total_rows($table);
$config['per_page']= 20;
$config['uri_segment'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = floor($choice);
$config['base_url']= site_url((string)$this->uri->segment(1));
$this->pagination->initialize($config);
$this->data['paginglinks'] = $this->pagination->create_links();
$this->data['EventsList'] = $this->mdl_home->get_with_limit($table,$config["per_page"], $offset,'EventDate DESC');
}
//this method load photo album page
private function _album_page(){
$table = 'aps_photo_album';
$offset = ($this->uri->segment(2) != '' ? $this->uri->segment(2): 0);
$config['total_rows'] = $this->mdl_home->total_rows($table);
$config['per_page']= 12;
$config['uri_segment'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = floor($choice);
$config['base_url']= site_url((string)$this->uri->segment(1));
$this->pagination->initialize($config);
$this->data['paginglinks'] = $this->pagination->create_links();
$this->data['AlbumList'] = $this->mdl_home->get_with_limit($table,$config["per_page"], $offset,'AlbumID DESC');
}
public function download_documents($file){
$this->load->helper('download');
$path = './assets/uploaded_documents/'.$file;
force_download($path, NULL);
redirect(base_url());
}
}
This is my Form Validation Library file
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation{
function run($module = '', $group = ''){
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
and this is my Users Controller file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends MX_Controller
{
public function __construct()
{
parent::__construct();
//load financial year
$this->load->model('mdl_users');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
redirect('Dashboard');
}else
{
$this->login();
}
}
//this function used to show login form view in template one column layout view
public function login()
{
$data['view_file'] = "loginform";
$data['module_name'] ='Users';
$data['page_title'] = 'Login Page';
$this->load->module('Template');
$this->template->login_layout($data);
}
//after entering the login values login form submit value handled by this function or method
public function validate_login()
{
$this->form_validation->set_error_delimiters('<div style="color:red;">', '</div>');
$this->form_validation->set_rules('username', 'Username', 'required|max_length[30]|xss_clean');
$this->form_validation->set_rules('password','Password', 'required|callback_verify_login');
if ($this->form_validation->run($this) == FALSE)
{
$this->index();
}
}
//this function for callback validation after submit button press
public function verify_login() {
$username = $this->input->post('username');
$password = $this->input->post('password');
//this method is in model folder/directory which interact with database https://www.youtube.com/watch?v=8fy8E_C5_qQ
$user = $this->mdl_users->check_login_authentication($username, $password);
if(empty($user)){
$this->form_validation->set_message('verify_login', 'Sorry the details you provided have not been found');
return FALSE;
}else{
//set user data in session and redirect to dashboard
$data = array(
'username' =>$user->UserName,
'userid' => $user->UserID,
'logged_in' => TRUE
);
$this->session->set_userdata($data);
redirect('Dashboard');
}
}
//logout function
public function logout()
{
$this->session->sess_destroy();
redirect('Users');
}
}

Resources