tags:

views:

1226

answers:

5

I am destroying all session var in logout.php and calling it when user click on logout, what is user does not click on logout.php but directly close the browser. how can i delete session then???

+2  A: 

You cannot. However, session cookies are usually send without an expire time which means they are deleted when the browser is closed, so the session is lost anyway.

ThiefMaster
A: 

You can set an expiration time for the session data, test it with each session_start call and destroy the session if it’s expired:

session_start();
if (!isset($_SESSION['EXPIRES']) || $_SESSION['EXPIRES'] < time()+3600) {
    session_destroy();
    $_SESSION = array();
}
$_SESSION['EXPIRES'] = time() + 3600;
Gumbo
That's a bit awkward solution, don't you think? There's a built-in function for that!
GuidoH
$_SESSION['EXPIRES'] ....'EXPIRES' is specific or I can place my session var name??
nectar
@GuidoH: You should better check what `session_cache_expire` actually does.
Gumbo
@Piyush: No, *EXPIRES* is a regular session datum like any other value in `$_SESSION`. I just thought “expires” is an appropriate name for such a value. But you can name it whatever you want.
Gumbo
Ok, but how is the session going to be started, since the browser's been closed and therefore the session cookie (which is a session cookie) is now destroyed on the client?
Marc B
what it dose $_SESSION['EXPIRES'] = time() + 3600; ???
nectar
@Piyush: It sets the expiration time to 3600 seconds in the future. So the session will expire after one hour of inactivity.
Gumbo
A: 

The session can be set to simply expire the server doesn't hear from the client after a certain period of time. That's the direction you want to be looking in...

Martin

Martin Milan
A: 

PHP sessions should automatically expire when the browser window closes providing you do not modify the Session Cookies expiration time. If this is not happening then I would assume that you have modified this in some way and we would require further details to assist.

Matt Weldon
A: 

If you do not set an expire time for the session cookie, it will be deleted when the user closes the browser, which has the same effect for most practical purposes (unless you are worried about storage or security). If that is not good enough, you could set sessions to expire very quickly and periodically refresh them via AJAX.

Tgr