Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroy PHP Session on closing

Tags:

php

session

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.

like image 904
sikas Avatar asked Nov 10 '10 16:11

sikas


People also ask

Is session destroy when browser is closed?

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.

Is session destroyed when browser is closed PHP?

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.

When close tab session is destroy?

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.

How can destroy session after some time in PHP?

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.


2 Answers

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.

like image 131
superfro Avatar answered Sep 20 '22 16:09

superfro


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.

like image 29
Treffynnon Avatar answered Sep 18 '22 16:09

Treffynnon