note 29207 deleted from function.setcookie by tularis
| From: | tularis@php.net | Date: | Sun, 02 Jan 2005 14:27:33 +0000 |
| Subject: | note 29207 deleted from function.setcookie by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-82599@lists.php.net to get a copy of this message | ||
Note Submitter: Lebbeous Weekley
----
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.