Destroy PHP Session on closing

前端 未结 5 1649
一生所求
一生所求 2020-11-27 06:35

I have created a simple login page which is based on the sessions.

session_start();

and added a logout page that contains this



        
5条回答
  •  北海茫月
    2020-11-27 07:19

    Server can't detect browser or tab closed, you could use Javascript or Ajax but sorry I don't have knowledge about that.

    My suggestion is use Session Timeout, so session will be destroyed if there's no action from user. This is an example :

    // destroy every 2 minutes
    
    if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 120)) {
        // last request was more than 2 minutes ago
        session_destroy();   // destroy session data in storage
    }
    $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
    
    // end of code
    

    Hope this help you

提交回复
热议问题