PHP Session expire after some minutes of inactivity?

'm developing a web app with PHP. The requirement is that the user has to login into the website. I'm using session variables for this. The session should expire after 10 minutes of inactivity and the browser have to forward to the login page. At the moment I'm not sure if I can solve it with the following php functions:

session_cache_expire(10); //should expire after 10 minutes inactivity

But I'm not sure if this expires the session after 10 minutes inactivity. I guess it will expire in general after 10 minutes. If it does it, how can I call an session exit handler?

The other way is to log the current time at each activty. How can I log touch events in Chrome? Is this possible? Without touch event logging it makes no sense.

asked May 28, 2015 in PHP by rajesh
0 votes

1 Answer

0 votes


As per @serakfalcon's suggestion, manage the last-request time in the session itself. To log the user out* at the front end after inactivity you will need to use some Javascript. Javascript isn't my strongest skill, but we've used something like this:

        window.location = '/loginpage';
    }, 600000);

Just remember that if you're using any AJAX that you'll probably want to have the timer reset when the request is made/completed. All that this does is start a counter for 10 minutes that will then redirect the user to the /loginpage URI. Obviously browsing away (ie remaining active) will prevent the redirect

* Not actually log them out, but rather redirect the user to the login page when the session has already expired at the server. It would be wise to make the JS redirect a few seconds at least longer than the PHP session expiry so that you don't accidentally renew the session with the call here

answered May 28, 2015 by rajesh