Automatically re-direct a user when session Times out or goes idle

前端 未结 3 1622
无人及你
无人及你 2020-12-08 17:43

I want to have a timer going to run every 3 minutes on the page (javascript), to detect if a php session ($_SESSION) has timed out... and if so, redirect them automatically.

3条回答
  •  眼角桃花
    2020-12-08 18:38

    You could use a simple meta refresh:

    
    

    Or you implement a timeout with PHP:

    session_start();
    if (isset($_SESSION['LAST_REQUEST_TIME'])) {
        if (time() - $_SESSION['LAST_REQUEST_TIME'] > 180) {
            // session timed out, last request is longer than 3 minutes ago
            $_SESSION = array();
            session_destroy();
        }
    }
    $_SESSION['LAST_REQUEST_TIME'] = time();
    

    Then you don’t need to check every 3 minutes if the session is still valid.

提交回复
热议问题