10

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.

3
  • Have you seen in database table entry of logged in user is added or not in table ci_sessions ? And JSON array for column you stored Commented Dec 13, 2011 at 18:40
  • Use this native session library: <github.com/alamops/codeigniter_native_session>
    – AlamoPS
    Commented Jul 23, 2014 at 14:44
  • I turn off CSRF and XSS countermeasures in config.php when I'm developing. Also turn off security for cookies in Dev. Just don't push your Dev config.php file to Prod.
    – b_dubb
    Commented Jan 16, 2017 at 20:56

7 Answers 7

7

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.

4
  • Thanks, that seems to have fixed the issue! I prefer using native PHP sessions anyways.
    – jswat
    Commented Mar 15, 2010 at 18:32
  • Glad it helped. I'm not overly enamoured with Codeigniter's session handling! Commented Mar 15, 2010 at 18:36
  • I have the same issue with a new CI project I'm doing.. It's very strange as all other projects work fine with the session in the DB.. using native works, but it's frustrating..
    – Antony P.
    Commented Dec 22, 2012 at 9:12
  • 1
    Just want to say a HUGE thank you for this pointer. I can now sleep after hours of battling against CodeIgniter's abysmal session library.
    – Owain Reed
    Commented May 11, 2015 at 0:45
6

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.)

0
3

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;
1
  • I have this table and saving data to it with empty sessions.Session data not available.Then I put $config['sess_use_database'] = FALSE; . Then worked fine. Commented Apr 1, 2013 at 19:20
0

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...:)

1
  • Thanks, but Codeigniter doesn't use traditional PHP session. It actually stores it's 'session' data in cookies (or in a database if you have it set up). Therefore, there is no need to add the session_start() code. The solution John McCollum gave fixed my issue. Thanks though!
    – jswat
    Commented Aug 5, 2010 at 19:15
0

Mind to check your cookie name. It can't have dots in it.

Bad cookie: .domain.com

Good cookie: domaincom

0

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

0

Codeigniter 3.0 in ubuntu displays only Array ( [__ci_last_regenerate] => 1522220387 )..remaining sessions are not displayedenter image description here

1
  • Images of code are highly discouraged. Please edit your answer and paste the code as plain text.
    – baduker
    Commented Mar 28, 2018 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.