How to log user out due to inactivity

前端 未结 7 633
借酒劲吻你
借酒劲吻你 2021-01-01 04:21

Pure, server-side PHP. Every time a user submits a form, I update a \'last activity\' time in the database.

I want to make a periodic check and force logout inactiv

相关标签:
7条回答
  • 2021-01-01 05:01

    With each login, you need to keep track of the Session Start Time, which can be done like this:

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

    With each user request to perform any operation, you need to run this script to monitor inactivity.

    <?php
    session_start();
    $TimeOutMinutes = 15; // This is your TimeOut period in minutes
    $LogOff_URL = "login.php"; // If timed out, it will be redirected to this page
    
    $TimeOutSeconds = $TimeOutMinutes * 60; // TimeOut in Seconds
    if (isset($_SESSION['SessionStartTime'])) {
        $InActiveTime = time() - $_SESSION['SessionStartTime'];
        if ($InActiveTime >= $TimeOutSeconds) {
            session_destroy();
            header("Location: $LogOff_URL");
        }
    }
    $_SESSION['SessionStartTime'] = time();
    ?> 
    
    0 讨论(0)
提交回复
热议问题