Welcome Guest, Not a member yet? Register   Sign In
Solution to session data loss when using AJAX
#1

[eluser]intractve[/eluser]
Hi All,
This post is a compilation of the steps taken to fix the issue of session updates (using the in-built CI_Session library) while using AJAX in your pages.
(This post assumes you are using the CI session with a database and not just cookies. This solution may work for just cookies too, I am not sure though.)

The CI Session library automatically updates the session information i.e changes the session_id (by default) every 5 mins
(configurable via config.php -> $config['sess_time_to_update']).
While this is a very useful security feature to thwart session hijacking, it can prove to be a bone when using AJAX.

When static pages are accessed, the CI session update does not interfere with regular operation and the new session id is updated in the cookie set in your system, and all is well. But when an AJAX operation is done (get or post), the session is updated and a new session id is generated (and updated in the database) but the cookie in the browser is not updated, so the next time the browser requests a new page it sends a cookie with the wrong session id and the session becomes invalid resulting in loss of session data (person getting kicked out if logged in).

I do not take credit for any of the solutions below, I'm just putting it in one place so that people do not waste as much time as I did trying to solve this. I have given due credit to the posters of the solution but if I have gotten it wrong or not credited you please let me know and I will update the post.

Step 1:
In constants.php (application/config/) you have to add the following line to define an AJAX request.
Taken from WebLee (http://www.weblee.co.uk)
Code:
// Define Ajax Request
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
//

Step 2:
Create a new file under (application/libraries) called MY_Session.php
The word MY_ should correspond to whatever you have defined in config.php as your prefix for class extension ($config['subclass_prefix'])
Paste the following into that file.
Solution by WanWizard
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Session extends CI_Session
{

/**
* Update an existing session
*
* @access    public
* @return    void
*/
    function sess_update()
    {
       // skip the session update if this is an AJAX call!
       if ( !IS_AJAX )
       {
           parent::sess_update();
       }
    }

}

/* End of file MY_Session.php */
/* Location: ./application/libraries/MY_Session.php */

And that's it!


What we have done is extended the Session Library overriding the update_session function which is responsible for creating new Session ID's and telling it to only update the session if the current call is not AJAX.

I have tested this and it works,

If there are any errors or better solutions feel free to post them below, so all can benefit from it.


Merry Christmas and Happy Holidays everybody...
:-)

--
George


Messages In This Thread
Solution to session data loss when using AJAX - by El Forum - 12-17-2009, 06:15 AM
Solution to session data loss when using AJAX - by El Forum - 12-17-2009, 06:48 AM
Solution to session data loss when using AJAX - by El Forum - 12-17-2009, 07:12 AM
Solution to session data loss when using AJAX - by El Forum - 03-13-2010, 04:17 PM
Solution to session data loss when using AJAX - by El Forum - 05-10-2010, 06:33 AM
Solution to session data loss when using AJAX - by El Forum - 07-27-2010, 12:49 AM
Solution to session data loss when using AJAX - by El Forum - 10-11-2010, 03:31 PM
Solution to session data loss when using AJAX - by El Forum - 10-11-2010, 04:49 PM
Solution to session data loss when using AJAX - by El Forum - 10-11-2010, 05:29 PM
Solution to session data loss when using AJAX - by El Forum - 10-11-2010, 06:47 PM
Solution to session data loss when using AJAX - by El Forum - 10-12-2010, 12:33 AM
Solution to session data loss when using AJAX - by El Forum - 10-12-2010, 06:29 AM
Solution to session data loss when using AJAX - by El Forum - 10-12-2010, 07:52 AM
Solution to session data loss when using AJAX - by El Forum - 10-12-2010, 08:06 AM
Solution to session data loss when using AJAX - by El Forum - 10-12-2010, 08:13 AM
Solution to session data loss when using AJAX - by El Forum - 10-12-2010, 09:03 AM
Solution to session data loss when using AJAX - by El Forum - 10-12-2010, 10:01 AM
Solution to session data loss when using AJAX - by El Forum - 10-13-2010, 03:01 AM
Solution to session data loss when using AJAX - by El Forum - 10-13-2010, 03:18 AM
Solution to session data loss when using AJAX - by El Forum - 01-20-2011, 06:58 AM
Solution to session data loss when using AJAX - by El Forum - 05-18-2011, 05:00 PM
Solution to session data loss when using AJAX - by El Forum - 05-25-2011, 12:21 PM
Solution to session data loss when using AJAX - by El Forum - 05-25-2011, 03:33 PM
Solution to session data loss when using AJAX - by El Forum - 05-25-2011, 03:54 PM
Solution to session data loss when using AJAX - by El Forum - 05-25-2011, 07:33 PM
Solution to session data loss when using AJAX - by El Forum - 05-26-2011, 08:43 AM
Solution to session data loss when using AJAX - by El Forum - 07-11-2011, 06:10 AM
Solution to session data loss when using AJAX - by El Forum - 07-11-2011, 07:43 AM
Solution to session data loss when using AJAX - by El Forum - 07-11-2011, 08:56 AM
Solution to session data loss when using AJAX - by El Forum - 07-11-2011, 01:21 PM
Solution to session data loss when using AJAX - by El Forum - 08-20-2012, 12:05 PM
Solution to session data loss when using AJAX - by El Forum - 08-23-2012, 08:50 AM
Solution to session data loss when using AJAX - by El Forum - 08-23-2012, 10:20 AM
Solution to session data loss when using AJAX - by El Forum - 08-23-2012, 01:00 PM
Solution to session data loss when using AJAX - by El Forum - 08-23-2012, 01:04 PM



Theme © iAndrew 2016 - Forum software by © MyBB