Php:Cookie Expiry

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.

<?php
$value = time()+3600 . ‘:’ . $variable;
SetCookie(’Cookie_Name’,$value);
?>
Then when you receive the cookie, decode it and determine if it is still valid.

<?php
list($ts,$variable) = explode(’:',$Cookie_Name,2);
if($ts < time()) {

} else {
SetCookie(’Cookie_Name’,”);
}
?>

Tags: , , , , , , , ,

Leave a Reply

You must be logged in to post a comment.