CodeIgniter: Fix for session loss caused by Ajax

Working on a new project in CodeIgniter I’ve found that when the sessions are saved in the database, they lost all the information that was stored in them when switching between controllers and doing simultaneous Ajax calls.

The solution was in CodeIgniter repository. It’s a bug from several versions ago and quite common among IC developers.

To solve the problem, for now, we must replace “system/libraries/Session.php” with Areson CI Session.php and save the session table with the following SQL script:

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
    session_id varchar(40) DEFAULT '0' NOT NULL,
    ip_address varchar(45) DEFAULT '0' NOT NULL,
    user_agent varchar(120) NOT NULL,
    last_activity int(10) unsigned DEFAULT 0 NOT NULL,
    user_data text NOT NULL,
    prevent_update int(10) DEFAULT NULL,
    PRIMARY KEY (session_id),
    KEY `last_activity_idx` (`last_activity`)
);

More info:
Enable simultaneous AJAX requests to work with sessions

This entry was tagged as

5 responses to CodeIgniter: Fix for session loss caused by Ajax

  1. karan thakkar

    thanks a million. finally got something to cheer.

  2. You’re welcome ;)

  3. Roomi

    Thanks for the post but what would you say about creating our own MY_Session.php library to with this function:

    function sess_update()
    {
    // Do NOT update an existing session on AJAX calls.
    if (!$this->CI->input->is_ajax_request())
    return parent::sess_update();
    }

  4. m chuluq

    thank a lot, This resolve my problem during this…

  5. Niju

    I am still facing the problem with this.

    My user_data being written is null and the session gets destroyed when passed on to a new controller

Leave a Reply

Your email address will not be published.