deleting cookies and local browser time versus server time

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hello all -

Is there a way to get the local time from the user's browser? I didn't
see anything in the $_SERVER array.

For unsetting cookies, the PHP documentation page for cookies
recommends setting the expiration date to a point in the past.
However, I'm running into problems with time conflicts. Our server is
in a different time zone than the browser I'm developing on. I would
like to go in five-minute increments to test my code after the cookie
expires, and it took me forever to figure out why my cookies weren't
expiring in five minutes.

It's not a big deal because it's only a few hours, so I can set the
time back several hours when we actually go into production. Of
course, hard coding any time could fail, is the user is halfway around
the world. So ideally I could look at the browser's local time, and
set the cookie back appropriately.

Re: deleting cookies and local browser time versus server time wrote in news:0ba47cb1-6fbe-44ae-b328-b4415bf387b1

Quoted text here. Click to load it

Well, considering you're asking the $_SERVER for a local user variable,  
it won't be there.

That should also raise a flag for you - since PHP is parsed on the  
server, you in fact have no way of determining your user's local time  
using PHP.

Quoted text here. Click to load it

Don't involve the user local time at all.  Put the server time in a  
variable, and test THAT number in 5 minute increments.

Quoted text here. Click to load it

Again, PHP knows nothing about the browser client.

Re: deleting cookies and local browser time versus server time

Quoted text here. Click to load it

$_SERVER does seem to contain some values that come from the browser:


Or did the server decide whether the browser was GETting or POSTing ?

$_SERVER[REDIRECT_QUERY_STRING] => query_string_test=hello

Or did the server somehow put the string variables in the browser's
address bar?

Several others are listed in this page:

Quoted text here. Click to load it

Well, if I can know what page the user is requesting, what variables
they're sending, or what browser they're claiming to use, I don't see
any *theoretical* reason the browser couldn't also send a timestamp or
a timezone. Whether or not they do, and if I can get access to that
information from PHP, is what I'm trying to figure out.

Quoted text here. Click to load it

How can I test cookie expiration with that? The browser unsets cookies
based on what *it* thinks the time is, not what the server thinks the
time is.

In other words, if the server is in Pacific time, and I'm in Eastern
time, setting the cookie to expire on date() ( meaning, 'right now' )
looks to be 3 or 4 hours in the past to my browser. So, if the server
give a cookie to expire at 4PM today, which is the time in Seattle,
that expiration date is already in the past for me, since it's already

To work around this, I might add 3 hours while I'm in development, but
then what about people in Mountain or Central time once I'm in
production? I can hard code a number of hours, since I know the
difference between my timezone and the server's. Obviously I can't
assume a future user's timezone. It would be handy is the user's
browser told me their time or time zone.

Quoted text here. Click to load it

It knows what the browser claims to be:

$_SERVER[HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 5.0;
en-US; rv: Gecko/20071127 Firefox/

Or did the server decide this also?

Re: deleting cookies and local browser time versus server time wrote in

Quoted text here. Click to load it

Yep, my bad.  I guess I just tend to ignore any 'browser' information in  
my apps.

Quoted text here. Click to load it

I guess I'm having a bit of difficulty trying to understand.  Cookies  
are local/stored on the user browser, correct?  So with you in Los  
Angeles and I in New York, we'll never see each others' cookies.

I interpreted your problem as essentially keeping a file 'open' or  
'checked out' (insert your verb here) for 5 minutes before  
allowing/disallowing changes or something.  So, if you open a file at  
10amPST, and I want to open it a minute later (1pm EST), I should see  
that only 1 minute has passed (as opposed to 3 hours and 1 minute).

If that's the case, what I am suggesting is that when person A opens the  
document, you record the SERVER time in a database or text file.  Then  
when person B hits the same document, PHP compares the time elapsed on  
the server between when Person A and Person B accessed the document, ie:  
whether or not its been less than / greater than 5 minutes.

Sorry to be of so little help!

Re: deleting cookies and local browser time versus server time

Quoted text here. Click to load it

Perhaps I'm not understanding it properly myself. Let me try to
explain it.

The website gives a cookie to the browser. That cookie stores
information that applies only to that website. It's like asking the
browser to store information for the website, and return that
information when the browser returns to that website.

One of the properties of the cookie is that it has an expiry time. It
won't last forever; the time that it lasts is set by the website.
Since the expiry time may pass when the user is not browsing the site,
it's up to the browser to delete a cookie when it has expired. This
cookie deletion happens independently of the wesbite; the browser does
it on its own.

If we are two browsers looking at the same site, we won't see each
others cookies. You are correct there. However, if we are browsing the
same site, we will each have our own cookies that we each get from the

However, when a user returns to a website,  the website can see the
cookie(s) that it previously gave a browser, so long as they haven't
expired. If they have expired, the browser won't send them back to the
website. And therein lies the rub: if the browser and the server
disagree about what time it is, the server may be expecting the cookie
when the browser thinks it's too old.

So, if the server generates a cookie that is set to expire at
something like "date() + 5 minutes", a timezone change could trip that
up. For the server in California, the time may be 8 AM, so it gives
the cookie to the browser, with an expiration time of 8:05 AM. For the
browser in New York, it's already 11 AM ( if it's 8 AM in CA ) , so a
cookie set to expire at 8:05AM, the cookie is DOA.

It's like I'm in Portland and you're in New York. I want some
information from you, to get to the bank before 5 PM. For you, 5 PM
comes and goes, no information. Then, at 7PM New York time, you get
the information. You say, "Oh, it's past 5PM, it's too late."
Meanwhile in Portland, the local time is 4PM, because of the time zone
difference. You could call me and give me the information, but because
of the time zone difference, you and I disagree about what time it

Quoted text here. Click to load it

Actually that's a great idea. Instead of storing data in the cookie, I
can just store a unique token in it to identify returning users. I can
use that token to look up the actual data I want to store from the

Quoted text here. Click to load it

Well, it is helpful to discuss it with someone. If I can't explain my
situation properly, that probably means I don't understand it myself!

Site Timeline