Newbie cookies question

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!

Moderator: General Moderators

Post Reply
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

Newbie cookies question

Post 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));
}
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
Post Reply