User’s Inactivity Check and Logout in PHP

User’s Inactivity Check and Logout in PHP

It is an essential professional approach to implement all necessary checks while developing a web based application. Sometimes it is necessary to check user’s inactivity on application because may be user forget to log out or their might be some other reasons too for his inactivity for example cialis canada. So you should have to check if user is inactive for certain period of time, you can log him out and destroy session. Your this approach build user’s trust in your development skills and definitely he will feel secure while using your website or application. So here is a quick way to check user’s inactivity and then log him out and destroy application.

First of all we have to add a single line of code in our login page to add the time of login in session.

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

Then add following code in every page of your application to check user’s inactivity.

if( isset($_SESSION['last_acted_on']) && (time() - $_SESSION['last_acted_on'] > 30*60) ){
 session_unset(); // unset $_SESSION variable for the run-time
 session_destroy(); // destroy session data in storage
 header('Location: index.php');
 }
 else{
 session_regenerate_id(true);
 $_SESSION['last_acted_on'] = time();
 }

What will this code do?

First it take time from session that we have stored recently in above line, then it will take current time and subtract it from current time. If  the result is greater than 30 minutes (you can change as your wish) it will log out the user and redirect him to our login page and if the result is less than 30 minutes it will store new time in session that is our current time.

[sdm_download id=”43″ fancy=”1″]


4 responses to “User’s Inactivity Check and Logout in PHP”

  1. schorsch Avatar
    schorsch

    in the download is the code:

    if( isset($_SESSION[‘last_acted_on’]) && (time() – $_SESSION[‘last_acted_on’] > 60*10) ){

    in the tut is

    if( isset($_SESSION[‘last_acted_on’]) && (time() – $_SESSION[‘last_acted_on’] > 30*60) ){

    what is wrong?

  2. Abdullah Majid Avatar

    Not a big deal in it. It’s just a time limit you can change it according to your need.

  3. schorsch Avatar
    schorsch

    and what is wrong? 60*10 or 30*60 the 60 first oder secend?

  4. Mohammed Rishad Avatar

    No difference,But 60 should be there. you write 60*10 as 600.No problem

Leave a Reply

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