{"id":1851,"date":"2014-02-14T22:13:15","date_gmt":"2014-02-14T20:13:15","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=1851"},"modified":"2014-02-14T22:31:37","modified_gmt":"2014-02-14T20:31:37","slug":"ajax-sessions-fix-codeigniter","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=1851","title":{"rendered":"Ajax sessions Fix CodeIgniter"},"content":{"rendered":"<p>For the most recent stable release of CodeIgniter (2.1.3), there is a rather annoying simultaneous request problem that will kill active sessions. You might have experienced this yourself if you had a website or application with lots of AJAX requests or other simultaneous requests. The tell-tail sign was that your users would be logged out after the update session time had passed (5 minutes by default).<\/p>\n<p>$config[&#8216;sess_use_database&#8217;] = TRUE;<\/p>\n<p>$config[&#8216;sess_time_to_update&#8217;] = 300;<\/p>\n<hr\/>\n<h2>Solution 1 :<\/h2>\n<p>One of the problem solution, it was due to the sess_time_to_update parameter in config.php. CI use this to update the session ID to a new one. If the change happen in an ajax call, CI sends a new cookie to tell the browser the new session ID. Unfortunatly, browsers seems to ignore this cookie and keep the old session ID.<\/p>\n<p>We can fix it by setting the sess_time_to_update to sess_expiration in the config.<\/p>\n<pre>$config['sess_time_to_update'] = $config['sess_expiration'];<\/pre>\n<hr\/>\n<h2>Solution 2 :<\/h2>\n<p>The solution that seemed to work for most folks involved extending the session library with your own MY_Session.php library that overwrote the sess_update method with one that only executed the update method when not an AJAX request. Create a php file in \/application\/libraries\/ with the name MY_Session (or whatever prefix you set), paste this code there and that is all. This function will override the sess_update function in the session class, checking on every request if that request was made by ajax, skipping the sess_update function.<\/p>\n<pre>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r\n\r\nrequire_once BASEPATH . '\/libraries\/Session.php';\r\n\r\nclass MY_Session extends CI_Session\r\n{\r\n\r\n\u00a0\u00a0\u00a0\u00a0function __construct()\r\n\u00a0\u00a0\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0parent::__construct();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;CI-&gt;session = $this;\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0function sess_update()\r\n\u00a0\u00a0\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Do NOT update an existing session on AJAX calls.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (!$this-&gt;CI-&gt;input-&gt;is_ajax_request())\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return parent::sess_update();\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}\r\n\r\n\/* End of file MY_Session.php *\/\r\n\/* Location: .\/application\/libraries\/MY_Session.php *\/<\/pre>\n<p>&nbsp;<\/p>\n<p>You can either auto-load this library from <i>config\/autoload.php<\/i>:<\/p>\n<pre>$autoload['libraries'] = array( 'MY_Session');<\/pre>\n<p>&nbsp;<\/p>\n<p>Or, you can load it later:<\/p>\n<pre>$this-&gt;load-&gt;library('MY_Session');<\/pre>\n<hr\/>\n<h2>Solution 3<\/h2>\n<div class=\"post-content\">\n<p>If you\u2019ve used AJAX-heavy web apps built on a CI backend, you might have noticed premature session expiration, even if you\u2019re expiration was set to never expire ($config[&#8216;sess_expiration&#8217;] = 0; in application\/config\/config.php)<\/p>\n<p>This was apparently due to AJAX requests not regenerating sessions, and apparent collisions. Long story short, there was a patch introduced without much fanfare, which seems to be working with codeigniter 2.1.3 .<\/p>\n<p>Replace your system\/libraries\/Session.php file with the one found here (CI\u2019s git):<\/p>\n<p><a href=\"https:\/\/raw.github.com\/EllisLab\/CodeIgniter\/b211adee89f5fd2192051e9c0826146bd150f469\/system\/libraries\/Session.php\" target=\"_blank\">https:\/\/raw.github.com\/EllisLab\/CodeIgniter\/b211adee89f5fd2192051e9c0826146bd150f469\/system\/libraries\/Session.php<\/a><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>For the most recent stable release of CodeIgniter (2.1.3), there is a rather annoying simultaneous request problem that will kill active sessions. You might have experienced this yourself if you had a website or&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2841,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[191,359,272],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1851"}],"collection":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1851"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1851\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2841"}],"wp:attachment":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}