Forum Moderators: coopster

Message Too Old, No Replies

Php Beginner Need help

PHP if visited set cookie

         

cuce

7:10 pm on Feb 18, 2005 (gmt 0)

10+ Year Member



Ok here's the deal, I have a site, and on the front page, I want a section of my code to show but only the first time someone visits my site.

how would i do that?

grandpa

7:26 pm on Feb 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can use a cookie.

if (!isset($_COOKIE['MyCookie'])) {
setcookie("MyCookie", 'My Info", time()+7200, "/", "", 0);
// show something once
}
else
{
// show something else
}

What this is doing - the first time I visit your page, the cookie will not be set. That is detected in the statement
if (!isset($_COOKIE['MyCookie'])), meaning if the cookie MyCookie is NOT Set then set a cookie and show
something once, otherwise show something else (or do nothing).

The cookie in this example will expire after 2 hours. If you need a longer time frame, adjust the time parameter.
If you need something permanent, condsider storing a record of the visit in a database.