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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

home destroy session after some time ?

Status
Not open for further replies.

callshakeel

Programmer
Apr 11, 2003
10
IN
hi,
I'm registering a session for user of mysite, using fuctionsession_destroy();
session_start();
session_register("user");
$user = $username;

but i want that if there is no activity by user for last one hour then automatically destroy his session,

Is there any way to store session for some time,
just like for cookie


 
have a look at
Alternatively you can make use of database to store the session set time along with the user identification ID.
On invocation of the script, check the session set time for the user, compare it with the current time and do the necessary action (eg. redirect the user to Logout page etc).

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
There are 2 parameters for session garbage cleanup which tell PHP to perform cleanup every n% of session accesses:
session.gc_probability integer
session.gc_divisor integer (100 default)

Also set the expiration of your sesion in seconds with session.gc_maxlifetime integer

 
what I do is set a session var called "timeout" and I set it with the current time plus the timeout. Example:

$_SESSION['timeout'] = date() + 300; // 5 min.

so, in every page I have:

if date() > $_SESSION['timeout'] then SESSION_DESTROY()
header("Location: login.html");
else
$_SESSION['timeout'] = date() + 300; // Add 5 min.

(this is pseudo code, use php code according to this script)

 
sorry, the function to use must be time(), not date():

$_SESSION['timeout'] = time() + 300; // 5 min.

if time() > $_SESSION['timeout'] then SESSION_DESTROY()
 
Chacalinc
The psudo code you have mentioned above will not work as expected, because you will be actually comparing the values as a string and NOT as a date type.

As DRJ478 mentioned, set the value of session.gc_maxlifetime integer in php.ini file according to your needs.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top