PHP - Session destroy after closing browser

前端 未结 9 1459
陌清茗
陌清茗 2020-11-27 07:43

Though this question has multiple duplicates i could not find proper solution for me. Need Some help.

I have used ini_set(\'session.cookie_lifetime\', 0);

9条回答
  •  萌比男神i
    2020-11-27 08:00

    There are different ways to do this, but the server can't detect when de browser gets closed so destroying it then is hard.

    • timeout session.

    Either create a new session with the current time or add a time variable to the current session. and then check it when you start up or perform an action to see if the session has to be removed.

    session_start();
    $_SESSION["timeout"] = time();
    //if 100 seconds have passed since creating session delete it.
    if(time() - $_SESSION["timeout"] > 100){ 
        unset($_SESSION["timeout"];
    }
    
    • ajax

    Make javascript perform an ajax call that will delete the session, with onbeforeunload() a javascript function that calls a final action when the user leaves the page. For some reason this doesnt always work though.

    • delete it on startup.

    If you always want the user to see the login page on startup after the page has been closed you can just delete the session on startup.

    and there probably are some more.

提交回复
热议问题