简体   繁体   中英

how to make session ends after closing browser

i used a simple session to create a login system,

<?php
    session_start();
    $_SESSION['username'] = $username;
?>

this is just a part of the code but it is what generate my session , it works fine until now, the problem is that this session don't end with browser closure, i tryed to login the close the browser , but when i visit the website again i find that i'm still logged in, the only way to logout is using a log out system, what i want is an automatic way to close the session on browser's closure, thanks in advance

Simple Answer: You can't. The session exists on the server and the browser exists on the client's machine. The browser does not notify the server when it is closing. In many cases (ex: a computer failure or network disconnect), the browser is unable to notify your server even if it wanted to.

Complex Answer: You could set a very low session timeout time (ie: 5 minutes) and update it every time the user pings the server, but this will log people out if they've been away from their keyboard for more than your timeout time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM