Elen
Elen

Reputation: 2343

PHP session time out on browser open

I know there are many threads regarding PHP sessions while ajax queries etc... but my problem is,

I have an ajax grid (build after the page load), which I allow to edit only when use is logged on. I don't mind for session to be not checked until user actually change the page (then valid_session.php is called), but I have an issue, when next day user opens the browser on the same page - the grid is still editable! obviously if I refresh the page, then user get logged out.

I have no-cache set on my pages, but browsers (in particular chrome) don't reload it on open.

I can't get my head around as how to force it to refresh on reopen. please guide me to the right direction...


EDIT

BTW - I found a way to handle this. I simply call session_destroy(); in session_destroy.php on unload() via $.get():

$(window).unload(function() {
    $.get('session_destroy.php', function(data) {
      alert(data); // alerts me of some var set to 0 - meaning session is destroyed.
    });
});

Upvotes: 1

Views: 492

Answers (2)

Scott Stevens
Scott Stevens

Reputation: 2651

One solution is to set a "last refreshed" cookie, and have a javascript setInterval() which checks if the cookie is older than, say 20 minutes. If it is, the javascript triggers a refresh. Of course, you still need to log them out after the inactivity period.

Upvotes: 1

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

To log out the user actively i think you should do some kind of polling and then trigger a logout automatically when the session expire. Or print an error message like "Changes done to this page will not be saved as the session has expired". Obviously the grid can't now "By magic" that the session has expired, you have to tell it somehow. In any case even if the grid it's still editable, it shoul dbe impossible to save changes, otherwise there is a design flaw (like not checking if the user is logged in before saving)

Upvotes: 2

Related Questions