How do I expire a PHP session after sometime when user is not active

php session expire after 5 minutes
how to set session('expire time in php)
php session timeout inactivity
session timeout php w3schools
php session timeout warning
unset($_session variable after time period in php)
php check session timeout
php session never expire

i want php session to be expired if there is no activity on the page for more than 10 to 20 minutes. Or user is not available for more than 20 min.Say we are taking an example of login, user logged in and after 20 min if there is no activity , it should expire the session and redirect to login page again.

Use Jquery

html or php page :

<body id="homepage" onload="set_interval()" onmousemove="reset_interval()" onclick="reset_interval()" onkeypress="reset_interval()" onscroll="reset_interval()">


//user login sessions
var timer = 0;
function set_interval() {
  // the interval 'timer' is set as soon as the page loads
  timer = setInterval("auto_logout()", 300000);
  // the figure '10000' above indicates how many milliseconds the timer be set to.
  // Eg: to set it to 5 mins, calculate 5min = 5x60 = 300 sec = 300,000 millisec.
  // So set it to 300000
function reset_interval() {
  //resets the timer. The timer is reset on each of the below events:
  // 1. mousemove   2. mouseclick   3. key press 4. scroliing
  //first step: clear the existing timer
  if (timer != 0) {
   timer = 0;
   // second step: implement the timer again
   timer = setInterval("auto_logout()", 300000);
   // completed the reset of the timer
function auto_logout() {
  // this function will redirect the user to the logout script
  **window.location = "index.php?opt=Logout";**




How to expire PHP sessions after a set period of time., //Start our session. session_start();. //Expire the session if user is inactive for 30. Here is a quick drill-down of the session expiry code above: We start our session with session_start(), like always! Remember: You cannot access session variables until the session has been started. For this example, I set the expiry limit to 30 minutes. You can change this number to 40 minutes or 60 minutes

Store the last request made time in session

  $_SESSION['timeout'] = time();

In every request happening, check how long ago they made their previous request (10 minutes in this example)

  if ($_SESSION['timeout'] + 10 * 60 < time()) {
     // destroy session & logout
  } else {
     $_SESSION['timeout'] = time();

User Login Session Timeout Logout in PHP, User Login Session Expiration Logout​​ This logout. php page will “unset” logged-in user session and check for the status of the session_expired flag. If it is set, then the login session timeout message will be displayed to the user. [RESOLVED] set php session to never expire 1042 ok so this is what i have, i have a login script that checks an imap server for a user, if the user has an account then he gets access to the private pages, here is the first part;

The Client-side solution:

Your page:

<script type="text/JavaScript">
    var idleRefresh;
    idleRefresh = setTimeout("location.href = 'unset.php';",30000);
    windows.onmousemove = function() {
        idleRefresh = setTimeout("location.href = 'unset.php';",30000);

unset.php: (Unset all session variables / specific login variables, and redirect user to login page)

    header('Location: login.php');

Automatic Logout after 15 minutes of user Inactivity using PHP , How to destroy session in php if user inactive since last 15 minutes. If user inactive for last Duration: 3:36 Posted: Apr 4, 2016 Before I explain what we did, it’s important to understand how PHP handles session data; in particular, when sessions expire and are subsequently cleared from the server. Since PHP supports multiple forms of session storage (file, database, etc.), this post will assume the default storage mechanism: the file system.

I would have added this add a comment to Arun's jquery solution, but don't have 50 reputation yet. I had to make some modifications to his code to get it working, for example, this:

timer = setInterval("auto_logout()", 300000);

Needs to be changed to this:

timer = setInterval( function(){ auto_logout() }, 300000);

I have included the fully revised code below:


<body onload="setTimer()" onmousemove="resetTimer()" onclick="resetTimer()" onkeypress="resetTimer()" onscroll="resetTimer()">

And Javascript is:

// Variables: timer, timeout  
var t, to = 10000;

// Setup the timer
function setTimer() {
    t = setInterval( function(){
    }, to);

// Reset the timer
function resetTimer() {             
    if (t > 0) {

// Logs user out
function logout() {
    window.location = "login.php";

As Arun has mentioned, change timeout (to) to whatever you need. In the above example I have it set to 10000, which is only 10 seconds.

Hope this helps - and thanks again for Arun for posting the original code.

Implementing Session Timeout With PHP, For example, if you Google php session timeout, you'll find no shortage Soon after, some users began complaining that they were being logged out too soon. how PHP handles session data; in particular, when sessions expire and Their role is to compute the probability on whether or not the garbage  Settings session.cookie_lifetime=0 (0 by default) should be enough in php.ini If you don’t have access to php.ini, you need to use ini_set(). Be sure to call ini_set() before starting a session.


var reset = 60000;
var timer = reset;

$('body').on('click keyup mousemove', function(){

function ct(){
    $('#so').text(timer/1000+' Sec. SIGN OUT');
        window.location = 'logout.php';

setInterval('ct()', 1000);
  background-color: tomato;
  padding:8px 20px;
  border-radius: 4px;
  text-decoration: none;
h3, #so{font-family: Segoe UI;}
<!-- LIBRARY -->
<script src=""></script>

<!-- HTML CODE -->
<a href="logout.php" id="so">SIGN OUT</a>

PHP : Auto expire session / Auto logout after specific time · GitHub, PHP : Auto expire session / Auto logout after specific time. gistfile1.php $​idletime=60;//after 60 seconds the user gets logged out Not working in codeigniter! i have a Simple Chat script of php it has no database it work fine. but it create and store all chats in a file msg.html i want to delete these chats after some time.

Automatic Logout after inactive for 15 minutes - PHP, Automatic Logout after inactive for 15 minutes As galen mentioned, when the user logs in, you can store the time in session or a db table, then whenever the Check for expiration first; if the user is within the time limits, refresh the time limit. If session.cookie_lifetime is 0, the session cookie lives until the browser is quit.. EDIT: Others have mentioned the session.gc_maxlifetime setting. When session garbage collection occurs, the garbage collector will delete any session data that has not been accessed in longer than session.gc_maxlifetime seconds.

Session Functions - Manual, When working on a project, I found a need to switch live sessions between I have been using sessions for quite a while without problems. Setting the session variables after the execution of the script i.e. in __destruct function, will not work. May you are asking why not use a cookie to pass the SESSION_NAME along  Expire a session after certain amount of time of user inactivity. XTREEMMAK I would like a script or a way for the user session to expire if the user has remained inactive for a certain amount of time, where the time will reset to limit should the user move.

session_unset - Manual, If $_SESSION is used, use unset() to unregister a session variable, i.e. unset all session variables. it does not destroy the the session would still be active. sometimes you might have problems even if using both session_unset and session_​abort · session_​cache_​expire · session_​​cache_​limiter  So; Session variables hold information about one single user, and are available to all pages in one application. Tip: If you need a permanent storage, you may want to store the data in a database. Start a PHP Session. A session is started with the session_start () function. Session variables are set with the PHP global variable: $_SESSION.