Destroy session when exiting the page [duplicate]

-5

I have a system for budgeting, and I need to see a confirmation message when you close the page, if you really want to leave the page. If she confirms, destroy the session.

Session I need:

$_SESSION['produtos']
    
asked by anonymous 11.07.2018 / 15:54

1 answer

6

The user's session ID is given by a cookie - by default, the cookie name is PHPSESSID . If the user loses the cookie , he will lose the session, as simple as that.

So, for the session to stay alive only as long as the user has the browser open, you just have to set up for the cookie to survive as long as the user keeps the browser open.

At runtime, you can set this with the session_set_cookie_params function, or you can change your php.ini accordingly:

void session_set_cookie_params ( int $lifetime [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]] )

The first parameter sets the lifetime of the cookie session, in seconds. If the value 0 is entered, the cookie will expire when the browser is closed.

In addition, options such as "Remember me" during login will only set a time other than 0 for cookie lifetime, so it will persist in the cookie browser even when it is closed.

As for the confirmation message to close the page, you should do this with JavaScript, unrelated to PHP. Something like, for example, listening to the unload window event:

window.onbeforeunload = function() {
    return "Gostaria mesmo de sair?";
};

Guilherme Credits

  

Maybe not exactly that, because I tried to reproduce and I could not. I'll look for more details, but if anyone knows, just let me know that I've already edited.

    
11.07.2018 / 16:08