So, I have set up a login page that verifies the user's credentials, and then sets codeigniter session data 'email' and 'is_logged_in' and a few other items. The first page after the login, the data is accessible. After that page, I can no longer access the session data. In fact, if I try reloading that first page, the session data is gone.
I have tried storing it in the database, storing it unencrypted (bad idea I know, but it was for troubleshooting), and storing it encrypted. I have autoloaded the session library in config.php.
Here's an example of the code I'm using to set the session data:
$data = array(
'email' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
And to retrieve it, I'm using :
$this->session->userdata('email');
Or
$this->session->userdata('is_logged_in');
I've done lots of work with PHP and cookies, and sessions before, but this is my first project with Codeigniter and I'm perplexed.
Could it have something to do with directory issues? I have the login page and process controlled by a 'login' controller, and then it redirects to a 'site' controller.
Thanks for your help, and please let me know if I need to clarify anything.
Your code looks fine. I've had numerous issues with Codeigniter's session library, including something similar to what you mentioned.
Consider looking at the Native Session library to resolve your issue.
Just so anyone else trying to use CI's sessions has something to try:
I have been looking around for a solution to the same problem, and after many dead ends, I re-investigated my config.php
.
Therein, I noticed that $config['cookie_domain']
wasn’t set properly.
Upon setting this (and setting my cookie_prefix
to ''
), sessions now work correctly.
Since the cookie needs to be set to your domain in order to be pulled correctly, it was simply not finding a cookie reference for my session, and making a new one every time.
(Setting up database sessions helped immensely with figuring this out.)
If anybody encounters this problem, go to config/config.php and make sure $config['sess_use_database']
is set to TRUE. This will save all session data to your database -- which worked for me.
If you don't have a table named 'sessions', run this SQL command:
CREATE TABLE `sessions` (
`session_id` VARCHAR( 40 ) NOT NULL DEFAULT '0',
`ip_address` VARCHAR( 16 ) NOT NULL DEFAULT '0',
`user_agent` VARCHAR( 120 ) DEFAULT NULL ,
`last_activity` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0',
`user_data` TEXT NOT NULL ,
PRIMARY KEY ( `session_id` ) ,
KEY `last_activity_idx` ( `last_activity` )
) ENGINE = MYISAM DEFAULT CHARSET = utf8;
Im also new to code igniter. Juz to give an idea. Have u put session_start() in your controller? here is the example:
class Cuser extends Controller {
function Cuser()
{
parent::Controller();
session_start();
$this->load->helper('url');
}
}
maybe im wrong...but u can try it...:)
Mind to check your cookie name. It can't have dots in it.
Bad cookie: .domain.com
Good cookie: domaincom
Why you don't use ion auth for security and for session data... I always using that when i want to create a software with login. Also you need to create a table in your db with name session. And go to config and database files in Config folder and put true to the session where it wants for the database. You don't have read well the documentation
Codeigniter 3.0 in ubuntu displays only Array ( [__ci_last_regenerate] => 1522220387 )..remaining sessions are not displayedenter image description here