Automatic Logout after 15 minutes of inactive in php

后端 未结 8 779
悲&欢浪女
悲&欢浪女 2020-12-04 15:51

I want to destroy session if users are not doing any kind of activity on website. At that time after 5 users automatically redirect on index page. How is it possible? Is pos

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 16:20

    This code was included in the connection.php to ensure that the code is included in any page but you can implement on any page you want

    if (isset($_SESSION['user-session']) OR isset($_SESSION['admin-session']) ) {
    //then we are checking the activity sesssion $_SESSION['']
    if (isset($_SESSION['last_active'])) {
    
        //if the time is set then we check the difference
        $max_time=5*60; #number of seconds
        $now=microtime(date("H:i:s"));
        //Checking the last active  and now difference in seconds
        $diff=round(microtime(date("H:i:s"))- $_SESSION['last_active']); #the difference of time
        if ($diff>=$max_time) { #if the difference is greater than the allowed time!
            //echo "logging out couse the time is".$diff;
            header("location:logout.php");          
        }else {
            $time=microtime(date("H:i:s"));
        $_SESSION['last_active']=$time; #Updating the time 
        //echo 'More time added the time was!'.$diff;
        }
    }else{
        //if there is no last active then we create it over here
        $time=microtime(date("H:i:s"));
        $_SESSION['last_active']=$time;
    }}
    

提交回复
热议问题