note 29207 added to function.setcookie
| From: | Lebbeous at rack1 dot php dot net | Date: | Thu, 06 Feb 2003 02:30:55 +0000 |
| Subject: | note 29207 added to function.setcookie | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-43491@lists.php.net to get a copy of this message | ||
I'm not completely sure how appropriate it is to post Javascript in this forum :) , but since
the problem of differing client/server time when setting cookies keeps coming up ...
In situations where I have users log in or do anything with a form that leads to the generating of
the cookie on the next page, I have this Javascript function in the login page:
//
// getTimestamp()
// This function sets the value of a hidden
// variable in the login form to the current
// time, expressed in seconds since the
// epoch, according to the client's computer.
//
function getTimestamp(form)
{
var t = new Date();
var timestamp;
// Date.getTime() returns milliseconds,
// so divide by 1000 for seconds
timestamp = Math.floor(t.getTime() / 1000);
form.time.value = timestamp;
return true;
}
This of course requires a) a hidden variable called 'time' to be placed in the form, and
b) an onSubmit="return getTimestamp(this);" element in the form tag. Then the PHP script
can take $_POST['time'] and use it instead of time() in calculating your cookie's
expiration date.
And of course it also wouldn't work when you want to send a cookie that doesn't come after
a form submission, but still maybe some will find it useful. If some browsers meddle with the time
already to try to correct client/server discrepancies, a browser check could be added to the
Javascript or to the PHP.
--
http://www.php.net/manual/en/function.setcookie.php
http://master.php.net/manage/user-notes.php?action=edit+29207
http://master.php.net/manage/user-notes.php?action=delete+29207
http://master.php.net/manage/user-notes.php?action=reject+29207