1

I know that this is a question asked several times but I have search a lot to find a solution with no luck. Some of the URLs i have visited were:

https://xpapad.wordpress.com/2010/06/19/preventing-session-expiration-with-ajax/

Prevent session expired in PHP Session for inactive user

PhP Session does not refresh using AJAX

Refresh PHP SESSION var after AJAX request

Most of them suggest that in order for the SESSION to refresh you must place an AJAX request that is targeting a PHP script that starts a session:

window.setInterval( function() {
  $.ajax({
      cache: false,
      type: "GET",
      url: "refreshSession.php",
      success: function(data) {
      }
  });
}, refreshTime );

In the refreshSession.php you can do something like session_start() .

Unfortunately by doing this the SESSION does not refresh. Any ideas why?

PS: Before calling session_start() i am calling my custom session name like session_name('CUSTOMSESSID').

Thanks,

Christos

Community
  • 1
  • 1
christostsang
  • 1,701
  • 3
  • 27
  • 46
  • 1
    make sure the session cookie is being transmitted with the ajax request. if it's not, php will simply create a new empty session, and do so every time you do another ajax request. – Marc B Jul 07 '15 at 15:00
  • @marc B: How do i do this? – christostsang Jul 07 '15 at 16:25
  • use your browser's debug tools to look at the header on both ends of the ajax request. – Marc B Jul 07 '15 at 16:43
  • I can see the cookie inside the request headers of sessionRefresh.php but not inside the response headers.. How do i fix this? – christostsang Jul 07 '15 at 16:50
  • make sure you're calling session_name() EVERYWHERE. if you miss it in even one spot, that script will be using whatever your server's default name is instead. and if you're overriding the name everywhere, you might as well just make it the default name by putting it into php.ini anyways, saving yourself the trouble of having to override manually everywhere. – Marc B Jul 07 '15 at 16:50
  • Thanks a lot for the help Marc. I will implement this and let you know!! Cheers!! – christostsang Jul 07 '15 at 16:54
  • Marc, it seems that the session name in both php.ini and the configuration inside WHM is set to CUSTOMSESSID. Do i need to remove session_name('CUSTOMSESSID') from every script i start a session? – christostsang Jul 08 '15 at 07:19
  • shouldn't matter, as long as the names really are identical. – Marc B Jul 08 '15 at 14:25
  • Marc, it turns out that even if i reload the page the session is cleared by garbage collector after session.gc_maxlifetime value. And if i reload the page after the maxlifetime value the user logs out (since log in status is handle by the sessions).What is the case with this? Shouldnt be refreshed if i reloaded the page? – christostsang Jul 09 '15 at 06:39
  • depends on how many other users there are on the site. EVERY hit that causes a session_start() has a chance to start the GC, which would clean out ALL expired sessions, not just the ones related to the user that triggered the GC. – Marc B Jul 09 '15 at 15:41

0 Answers0