Automatic session timeout/logout using PHP after X Minutes of Inactivity/Idle time

PHP Automatic Session Expire after X Minutes of Inactivity/Idle time

 Automatic session timeout/logout using php


Session timeout is a notion and the only way you make you sure that no session ever will survive after X minutes of inactivity. Session timeout or Session expire depends on the server configuration or the relevant directives (session.gc_maxlifetime) in php.ini.


Typically the default is 1440 seconds(24 minutes), but you can alter the default to something else. Below are some Session configurations.


You can update this easily and without writing custom logic.


If your sessions are implemented with cookies (which they probably are), and if the clients are not malicious, you can set an time limit on the session duration by tweaking certain parameters. If you are using PHP’s default session handling with cookies, setting session.gc_maxlifetime along with session_set_cookie_params should work for you like this:


// server should keep session data for 1 hour
ini_set(‘session.gc_maxlifetime’, 3600);

// each client remember their session id for exactly 1 hour

session_start(); // ready to go!


You can also put this in .htaccess file with a slight change in syntax.


[sourcecode language=”plain”]
php_value session.gc_maxlifetime 3600

php_value session.gc_probability 1

php_value session.gc_divisor 1


session.gc_probability, and session.gc_divisor directives: PHP has garbage collection it uses to clean up sessions that have expired, otherwise on a site with a lot of users accessing the site could cause a huge amount of session files to be continually generated. Garbage collection does not happen automatically and needs to be incorporated into your system maintenance routines.


You can also make a custom script that automatically logout a user if user is inactive (not performed any action or idle) for X minutes.


1) When user logged in, start session, start session expiry time, like this:

[php] $_SESSION[‘expire’] = time() + X*60; [/php]

We took current time, added X minutes in it and stored this in session.


2) At every page check that if X minutes (for above script) have been passed or not make a file as include that in that page, like this:

If yes, clear session and logout, like this:

if(time() > $_SESSION[‘expire’]){
    $_SESSION = array();


And then redirect to login page.


3) In else statement (if X minutes have not passed), reset time (takes current time and add X minutes in it and restore in session named ‘expire’) stored in session, like this:

else { $_SESSION[‘expire’] = time()+X*60; }


and do nothing – don’t clear sessions, don’t redirect to login page, so that user may stay at website, as much time as he is active.


You can also do this purely using JavaScript. Start an countdown timer. Then wait for activity and reset this timer. If there is no activity and timer goes off, you can call your logoff sequence.

[code lang=”js”]

/* Resets the timer. The timer is reset on events
(mouse-move,mouse-click,key press,scrolling),
   these events occurs indicates that user is active on the application:

<body onmousemove="reset_interval()" onclick="reset_interval()" onkeypress="reset_interval()" onscroll="reset_interval()">

<script type="text/javascript">
//the interval ‘timer’ is set as soon as the page loads

var timer = setInterval(function(){ auto_logout() }, 20000);

// the figure ‘20000’ (20 seconds) indicates how many milliseconds the timer be set to.

//e.g. if you want it to set 5 mins, calculate 5min= 5×60=300 sec => 300,000 milliseconds.
function reset_interval(){

    //first step: clear the existing timer
//second step: implement the timer again
    timer = setInterval(function(){ auto_logout() }, 20000);
    //..completed the reset of the timer


function auto_logout(){

    //this function will redirect the user to the logout script
if(confirm("You have been logged out from the application, Press OK to login again!")){




Hope it will works, let me know by your valuable comments if you need any more assistance.