PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?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
Last edited by omidkamangar on Mon Jul 31, 2006 8:36 am, edited 1 time in total.
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.
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.
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.
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:
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.
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.