简体   繁体   中英

PHP Session issue on browser close

Here running into problem where I have requirement to clear user session when closing the browser. I have tried all the various option like setting session.cookie_lifetime=0 or session_destroy on browser close using onunload function. But nothing seems to destroy session when I open the browser next time.

I just googled a bit and I saw that in Chrome browser there is setting called ''Allow local data to be set' that has to be changed to 'Keep local data only until I quit my browser', when I do this it does not retain my session.

The real problem is I cannot ask each user to change the settings of the browser and then it would work accordingly, is there a way I can handle it in code using php or javascript. Any option is fine.

Have you tried checking for both cookie and session when your page loads? Something like this:

1) destroy cookie on unload 2) on page load check for both

if(isset($_COOKIE['user'] && $isset($_SESSION['user'] {
    //user is logged in
} else {
    //your code should fall here after user closes browser
    //because the cookie doesn't exist anymore.  Maybe you can even destroy the session too
    [session_destroy();]
    ...
    ...
}

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