Cookies Expiry Time

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

Cookies Expiry Time

Postby Web Guru on March 25th, 2008, 4:27 am

Problem
Short expiry cookies depend on users having their system clocks set correctly.

Solution
Don't depend on the users having their clocks set right. Embed the timeout based on your server's
clock in the cookie.

Code: Select all
<?php
$value = time()+3600 . ':' . $variable;
SetCookie('Cookie_Name',$value);
?>

Then when you receive the cookie, decode it and determine if it is still valid.

Code: Select all
<?php
list($ts,$variable) = explode(':',$Cookie_Name,2);
if($ts < time()) {
...
} else {
SetCookie('Cookie_Name','');
}
?>
User avatar
Web Guru
 
Posts: 75
Joined: March 24th, 2008, 7:59 am
Location: Lahore, Pakistan

Return to PHP / MySQL / XML

Who is online

Users browsing this forum: No registered users and 0 guests

cron