Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP session timeout

Status
Not open for further replies.

arm207

Programmer
Jun 13, 2006
26
US
Hi,
Is it possible to provide php session timeout functionality by modifying php.ini? How?

If the above is not possible, does anyone know of an example on how to do php session timeouts?
 
you can change the php.ini to create the equivalent of a time out:

set
session.gc_probability = 100
session.gc_maxlifetime = 600
session.cookie_lifetime = 600

the first line forces php to run garbage collection every time a session is started
the second line specifies the number of seconds that the session should endure
the last line specifies the number of seconds that a session cookie should endure.

or as an alternative that does not involve changing the php.ini file ...

you could store the last accessed time as a session variable and logout the user if they try to come back to the site after the expiry of a time period

Code:
session_start();
define ("TIMEOUT", 600);
if (isset($_SESSION['loggedin']):
  if (isset($_SESSION['lastaccess']) && (($_SESSION['lastaccess'] + TIMEOUT) > time())):
      $_SESSION['lastaccess'] = time();
      //login ok
  else:
    unset($_SESSION['loggedin']);
    unset ($_SESSION['lastaccess']);
    echo "You're session has expired. Please login again";
    display_login_form();
  endif;
else:
 echo "Please login";
 displayloginform();
endif;
 
in the example above, is TIMEOUT defined to be 600 seconds or minutes???
 
TIMEOUT is defined as 600 (no unit) but the use against unix timestamps means that it equates to seconds (i.e. 10 minutes).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top