Page 1 of 1

Newbie cookies question

Posted: Fri Dec 23, 2005 11:28 pm
by jaymoore_299
I'm using a cookie to keep track of visitors to a site, for this particular cookie I'm just using it to see if they have visited the site before.

I just need to be able to do the following:
1. if they already have the cookie, don't do anything
2. if they don't have the cookie, set a cookie with expiration date 24 hours.

would something like this work

to check if surfer has a cookie named 'visitor'

Code: Select all

if (!empty($_COOKIE['visitor']))
{

}
else

Code: Select all

{
setcookie ('visitor', '1', time() + (60*60*24));
}

Posted: Fri Dec 23, 2005 11:35 pm
by josh
You need to store the timestamp of their last visit in the cookie, and set it on each visit, then here's how you detect it:

if there is no cookie, or the timestamp in the cookie is older than 24 hours you know they haven't visited today

else - they have!


you had the idea down I think but if the user were to return sooner then 24 hours, it would not "reset" the expiration date in your example... please note in my example the expiration date should be either exactly 24 hours or more, but it is being set on every page view and it contains the current timestamp.