how to expire php session if user is inactive for 15 mins

后端 未结 6 569
无人及你
无人及你 2020-12-04 22:48

i have created one project in PHP, into which i am managing sessions.

I am creating session in my config.php file by writing following line of code.

         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 23:22

    You can use something like this

    # Session Logout after in activity 
    function sessionX(){ 
        $logLength = 1800; # time in seconds :: 1800 = 30 minutes 
        $ctime = strtotime("now"); # Create a time from a string 
        # If no session time is created, create one 
        if(!isset($_SESSION['sessionX'])){  
            # create session time 
            $_SESSION['sessionX'] = $ctime;  
        }else{ 
            # Check if they have exceded the time limit of inactivity 
            if(((strtotime("now") - $_SESSION['sessionX']) > $logLength) && isLogged()){ 
                # If exceded the time, log the user out 
                logOut(); 
                # Redirect to login page to log back in 
                header("Location: /login.php"); 
                exit; 
            }else{ 
                # If they have not exceded the time limit of inactivity, keep them logged in 
                $_SESSION['sessionX'] = $ctime; 
            } 
        } 
    } 
    

    But remember Function sessionX() MUST come after session_start()

    See details here

提交回复
热议问题