how to log off a session when the browser window is closed?
Moderator: General Moderators
-
leonardobii
- Forum Newbie
- Posts: 17
- Joined: Fri Sep 02, 2005 8:47 pm
how to log off a session when the browser window is closed?
So I created user authentication and session handling scripts, but I cant figure out how to log off a session when the browser window is closed? any suggestions?
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
from php manual
do more reading on, http://uk.php.net/sessionsession.cookie_lifetime integer
session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means "until the browser is closed." Defaults to 0. See also session_get_cookie_params() and session_set_cookie_params().
-
leonardobii
- Forum Newbie
- Posts: 17
- Joined: Fri Sep 02, 2005 8:47 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
-
leonardobii
- Forum Newbie
- Posts: 17
- Joined: Fri Sep 02, 2005 8:47 pm
-
leonardobii
- Forum Newbie
- Posts: 17
- Joined: Fri Sep 02, 2005 8:47 pm
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
this is useful to close session after some period of inactivity.
Code: Select all
<?php
session_start();
if (!isset($_SESSION["last_access"])){
$_SESSION["last_access"] = time();
}else{
//echo $_SESSION["last_access"]."<br />"; //last access time
//echo time()."<br />"; //current time
//echo time() - $_SESSION["last_access"]."<br />"; //difference between current time and last access time
if (time() - $_SESSION["last_access"] > 5*60){ //expire time set to five minutes
//echo "session deleted!!!";
session_destroy(); //destroys all session variables
setcookie(session_name(), "", time() - 10); //destroys the session itself which is actually transported via cookie
//unset($_COOKIE[session_name()]); //this also could be used to destroy the session cookie; alternative for the former
}
}
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
or you can just ask PHP to handle it automatically. The following are directives you can toggle anywhere you like (.htaccess, php.ini, your script)
session.gc_divisor
session.gc_maxlifetime
session.gc_probability
session.gc_divisor
session.gc_maxlifetime
session.gc_probability
-
leonardobii
- Forum Newbie
- Posts: 17
- Joined: Fri Sep 02, 2005 8:47 pm
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
through .htaccess: http://php.net/configuration.changes
through active php: http://php.net/function.ini_set
through php.ini: http://php.net/manual/en/ini.php#ini.list
through active php: http://php.net/function.ini_set
through php.ini: http://php.net/manual/en/ini.php#ini.list