I have created a simple login page which is based on the sessions.
session_start();
and added a logout page that contains this
session_destroy();
Now when I close the browser/page and reopen it, the values of the session are still there.
I want to know how to completely destroy the session on page/browser close.
Browsers deletes the session cookies when the browser is closed, if you close it normally and not only kills the process, so the session is permanently lost on the client side when the browser is closed.
Session in PHP is a way of temporarily storing and making data accessible across all the website pages. It will create a temporary file that stores various session variables and their values. This will be destroyed when you close the website.
On Firefox however, when I close it and reopen and try to access a secure page I still get allowed in, why could this be happening? Closing tabs does not end sessions only until the browser is closed will it destroy the session. Assuming you didn't tell the browser to always save sessions on close.
Destroying a PHP Session A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.
if you use:
session_set_cookie_params(0);
session_start();
Your session cookie will destroy when the browser is closed... so your session will be good until they close the browser. IE. You login, and you are logged in, you close the browser, re-open it, go to the site again, and you wont be logged in.
You will only be able to detect if the browser window has been closed using javascript at which point you might be able to trigger an Ajax request to perform a logout action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With