1

I am new comer with php codeigniter session. i have a issue about session expiration on my site. I am using ajax coll for laoding advertizing div on our site page and also add to cart feature to add multiple products on same page. but my cart added products is empty due to session getting expired after appro. 4-5 minutes. I have done lot of study about this issue. i think my site session is expired due to colling ajax and updating session id at same time.But i am not sure for that.

I have configured config.php file like -

$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration']  = 0;
$config['sess_encrypt_cookie']= FALSE;
$config['sess_use_database']= TRUE;
$config['sess_table_name']  = 'ci_sessions';
$config['sess_match_ip']    = FALSE;
$config['sess_match_useragent']= TRUE;
$config['sess_time_to_update'] = 300;

I have also created table(ci_sessions).

Please give me a solution to fix this issue.

Thanks.

shamon shamsudeen
  • 5,466
  • 17
  • 64
  • 129
  • codeigniter what version? – egig Apr 05 '13 at 08:40
  • @Charlie ,I am using codeigniter 1.7.2 . –  Apr 05 '13 at 08:44
  • Please Please ,Give simple solution of this issue because i very new comer with codeigniter –  Apr 05 '13 at 08:47
  • why dont just use newer version since you're new to CI ? – egig Apr 05 '13 at 08:48
  • @Charlie , Thanks for suggestion . But can not change my existing version due to some working company reason.**Please Give alternate solution** –  Apr 05 '13 at 08:54
  • 1
    you got two answer, still no right solution ?? – egig Apr 05 '13 at 09:01
  • @Charlie , Please Explain me , how to use MY_Session.php(/application/libraries/MY_Session.php) file after creating on controller.according to your first solution –  Apr 05 '13 at 09:25
  • the most simple solution is replace session.php with [this one](https://raw.github.com/EllisLab/CodeIgniter/b211adee89f5fd2192051e9c0826146bd150f469/system/libraries/Session.php), and done. If you prefer to extend the session.php, I am not sure, just ask the answerer. – egig Apr 05 '13 at 09:35
  • @Charlie , Ok sir i just trying this –  Apr 05 '13 at 09:42
  • dont use MY_Session.php – egig Apr 05 '13 at 10:04
  • @Charlie , Fatal error: Call to undefined method CI_Input::is_ajax_request() in C:\wamp\www\mysadagi_voicetongues\system\libraries\Session.php on line 365 –  Apr 05 '13 at 10:08

3 Answers3

5

There was a patch for session.php in CI last year that resolved the issue of ajax+session expire problem please the look at the following patch LINK HERE

Ravindra Shekhawat
  • 4,275
  • 1
  • 19
  • 26
4

Just make sure, there is two option.

1.Replace system\libraries\Session.php with this one

2.Extend your original session.php like @undefined answer.

If you do no.1 then dont do no.2, and vice versa.

I have no idea more than that. good luck !.

egig
  • 4,370
  • 5
  • 29
  • 50
1

This is a CI bug, sessions are destroyed when an Ajax request is sent, you can extend the CI_Session class and call the sess_update only when request is not Ajax.

class MY_Session extends CI_Session
{
    function sess_update()
    {
        // Listen to HTTP_X_REQUESTED_WITH
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] !== 'XMLHttpRequest') {
            parent::sess_update();
        }
    }
}

In order to load the MY_Session class, you can add this function to your config file:

function __autoload($classname) {
    if (strpos($classname, 'CI_') !== 0) {
        $file = APPPATH . 'libraries/' . $classname . '.php';
        if (file_exists($file) && is_file($file)) {
            @include_once($file);
        }
    }
}
Ram
  • 143,282
  • 16
  • 168
  • 197