How To Automatically Logout Inactive User in PHP


How To Automatically Logout Inactive User in PHP

Demo Download

In this tutorial, I will show you how to automatically logout inactive user in PHP.

If the user is inactive for more than 15 minutes (900 seconds) and does not click any other page then this user will be considered as inactive user and automatic logout after 15 minutes of inactivity in PHP.

You can set any time interval based on your application requirement, just multiply the minutes to 60. For example 30 * 60 = 1800 now you can set 30 minutes (1800 seconds) of user inactivity.

I presume that you already know about how to create user registration and login system in PHP.

If you do not have any idea about it then you check out my tutorial about it.

I will add code of automatic logout after 15 minutes of user inactivity using PHP in my PHP user registration and login script tutorial.

Default PHP Session Timeout

PHP login system uses the session mechanism to keep the user logged in. Generally by default the PHP session timeout is 24 minutes which is equivalent to 1440 seconds.

When you are using session, your PHP page should contain session_start() function in the beginning of the page.

This function create a new session or resumes the old one.

So that you can create new sessions variables or access them on different pages.

Last User Activity Timestamp

When the user logged in to the application, you will need to create a new session variable $_SESSION[‘last_timestamp’] = time(); which will contain user’s last activity timestamp.

Whenever a new web page is loaded or the current page is refreshed.

We will need to check the time difference between the current time and the user’s last activity time.

If the time difference is more than 15 minutes (900 seconds) then log out the user by simply destroying the session data and redirect user back to the login screen.

Following is the sample code that you will need to put in the top of on every page.

This code will check the user’s inactivity and log the user out from the application, if the inactivity is more than 15 minutes.

PHP Code:

<?php
session_start();

// Set the inactivity time of 15 minutes (900 seconds)
$inactivity_time = 15 * 60;

// Check if the last_timestamp is set
// and last_timestamp is greater then 15 minutes or 9000 seconds
// then unset $_SESSION variable & destroy session data
if (isset($_SESSION['last_timestamp']) && (time() - $_SESSION['last_timestamp']) > $inactivity_time) {
    session_unset();
    session_destroy();

    //Redirect user to login page
    header("Location: login.php");
    exit();
  }else{
    // Regenerate new session id and delete old one to prevent session fixation attack
    session_regenerate_id(true);

    // Update the last timestamp
    $_SESSION['last_timestamp'] = time();
  }
?>

The above code shows the way to log out the inactive user.

You will need to place this code in the beginning of all pages where you want to implement automatic logout feature.

Add HTML Meta http-equiv

Although the above PHP code will work but you will need to refresh the page to execute the PHP code.

So you can add the meta http-equiv refresh in the <head> section of your pages to force refresh the pages after 15 minutes.

Add the following code in your file.

<meta http-equiv="refresh" content="9001">

Although the demo of this tutorial is not possible. However, I have created a demo link that will show you how the session is automatically destroyed after 30 seconds.

For demo purpose, I am using only 30 seconds so that you do not need to wait longer than that.

You can also download the source code of complete login system that is using session to log out the user.

Just click on the download button of this tutorial to get the complete source code.

Conclusion

I hope by now you know how to automatically logout inactive user in PHP. You see how easy it is to check and log out the inactive user using PHP.

If you found this tutorial helpful, share it with your friends and developers group.

I spent several hours to create this tutorial, if you want to say thanks so like my page on Facebook, Twitter and share it.

Demo Download

Facebook Official Page: All PHP Tricks

Twitter Official Page: All PHP Tricks

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.

Leave a Reply

Your email address will not be published. Required fields are marked *