Page 1 of 1

Cookie Failure

Posted: Sun Jul 30, 2006 2:57 pm
by omidkamangar
Hello
I am a newbie to PHP and I have a problem with php.
I use cookies to count the number of visitors of my web site.
I use the following code :

Code: Select all

<?php
	//get cookies if any set
	$visited = $_COOKIE['visited'];
	$last_visited = $_COOKIE['lasttime'];
	$is_last_set = $_COOKIE['is_last_set'];
	if(!$is_last_set)	//if the visitor is revisiting site in less than 20 minutes do not set the cookie
			{
				setcookie("is_last_set","TRUE",time() + 1200 );
				setcookie("lasttime",date('l dS \of F Y h:i:s A'),time() + (10000 * 10000 ));
			}
	if(!$visited)
	{
		setcookie("visited","TRUE",time() + 1200 );
	}
	
?>
----------------
but when the cookies are set on the client(visited & is_last_set) the expire time is more than 60 minutes
instead of 20 minutes.
I wonder if anyone can help me

Posted: Sun Jul 30, 2006 3:05 pm
by RobertGonzalez
Are the client and server clocks in sync?

Posted: Mon Jul 31, 2006 8:15 am
by omidkamangar
Well I did not understand what you mean.
But when I run the php script on my own pc (running apache). It works fine but when I transfer it to the server hosting my site it does not work fine.
:cry:

Posted: Mon Jul 31, 2006 8:19 am
by panic!
What he means is, your local machine may be in (for example) London, but your server may be in California therefore your machine is reporting that it is Midday and the server in California is saying 5am.

hope that helps.

Posted: Mon Jul 31, 2006 8:20 am
by RobertGonzalez
Sorry for not being clear enough.

robincard, that is exactly what I meant. Thanks for clearing that up for me.

Posted: Mon Jul 31, 2006 8:21 am
by Oren
Everah, I don't see why the clocks should be sync?

Posted: Mon Jul 31, 2006 8:27 am
by RobertGonzalez
The expiry times of the cookie (set by the server, but placed on the client) can be affected by out of sync time settings. Another poster just had this issue resolved a few days ago by making sure the time on his computer was correct.

The Zend website uses a notice to inform you that if your computers clock is not set to the current time, that you may not be able to login because of this behavior. I would guess it mostly affects short term cookies.

Posted: Mon Jul 31, 2006 8:37 am
by Oren
Everah wrote:The expiry times of the cookie (set by the server, but placed on the client) can be affected by out of sync time settings. Another poster just had this issue resolved a few days ago by making sure the time on his computer was correct.

The Zend website uses a notice to inform you that if your computers clock is not set to the current time, that you may not be able to login because of this behavior. I would guess it mostly affects short term cookies.
Well, his code is messy, but it seems to me that he his checking this on the server side and not on the client side - he stores the time of the cookie creation in the ookie itself:

Code: Select all

setcookie("lasttime",date('l dS \of F Y h:i:s A'),time() + (10000 * 10000 ));
Therfore, the sync doesn't matter... Or maybe I just misunderstood him - the code is messy so I didn't read it all, just part of it.

P.S By "messy" I meant that I don't see why he his doing it this way, nothing else :P

Posted: Tue Aug 01, 2006 10:54 am
by omidkamangar
Hi
Thanks for your attention.
This is what I want tod do :
I want to make a counter for my web site so that I know how many people have visited it.I do this by setting a cookie on the client machine which expires within 20 minutes.Each time a page is requested it checks for this coockie.If it is found then it means that the user is reloading this page or comming from one of the other pages in a short time.
If the cookie is not found it means that this request is a new one and I update the database and add another user to the list.
But as you see in the code above, when I try to set the cookie for 20 minutes , it is set for more than 60 minutes.
I do not know what to do to make the clocks sync , because each time the client may be in a different country.I checked the PHP manual but got nothing.
And my problem is about the first cookie, the second is not important.
Because I am new to web programming I do not know what to do.

P.S : Because I am not a native english some of my sentences may be grammatically wrong .I do apologize for that.

Posted: Tue Aug 01, 2006 11:04 am
by feyd
If you send the "date" http header, it may help the browser know what time differences to use.

Posted: Tue Aug 01, 2006 11:23 am
by Oren
1. Store the creation time of the cookie within the cookie itself.
2. Set the cookie to expire only after a long time.
3. Each time the cookie cannot be found - increase the number in the database and create a new cookie with the creation time stored in it, if the cookie already exists - check the difference between the cookie's creation time and the current server time, if the difference is greater than 20 minutes - increase the number in the database too.
4. Update the time stored in the cookie to the current server time.