Admin panel is not working in Codeigniter. Session is working in codeigniter's controller but session is not working on transfer to other controller.
Because of this the admin panel is also not working and the product is not being added to the cart even on the website.
Here is the config code:
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update'] = 3600;
Here is the autoload code:
$autoload['libraries'] = array('database', 'session');
Here is controller code Home.php :
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('user','',TRUE);
}
public function index() {
$sess_array = array(
'id' => $row->id,
'login_name' => $row->login_name,
'user_name' => $row->user_name,
'department' => $row->department
);
$this->session->set_userdata('is_admin_login', $sess_array);
}
Here is another controller Dashboard.php :
public function index() {
$userid = $this->session->userdata['is_admin_login']['login_name'];
}
This problem sometimes comes because of the session. The solution to this problem is as follows -
Solution-1 :
You can use the all_userdata() method for retrieving all session data as an ASSOC array.
You can write that in your Home.php controller and you can send the returned value to the view.
$data['is_admin_login'] = $this->session->all_userdata();
$this->load->view('your/view.php', $data);
Solution-2 :
You can load the session library in the constructor of Dashboard.php
Example:
class Dashboard extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
// Rest of all controller's code here
}
Solution-3 :
In CodeIgniter 3.x if you just upgraded to PHP version 7.x,
go to system/libraries/Session/session.php at line 281 and replace :
If you just upgraded PHP version than you have to change some code in CodeIgniter 3.x
For this go to system/libraries/Session/session.php
and find below code ini_set('session.name', $params['cookie_name']);
and replace it with this code ini_set('session.id', $params['cookie_name']);
Solution-4 :
Also in Session.php find below code
if (isset($_COOKIE[$this->_config['cookies_name']])
&& (
!is_string($_COOKIE[$this->_config['cookies_name']])
OR !preg_match('/^[0-9a-f]{40}$/', $_COOKIE[$this->_config['cookies_name']]))
){
unset($_COOKIE[$this->_config['cookies_name']]);
}
Replace OR !preg_match('/^[0-9a-f]{40}$/', $_COOKIE[$this->_config['cookie_name']])
To OR !preg_match('/^[0-9a-f]{32}$/', $_COOKIE[$this->_config['cookie_name']])