Forum Moderators: coopster

Message Too Old, No Replies

Need help with cookies: Show section only once

         

IntegrityWebDev

7:49 pm on Mar 10, 2010 (gmt 0)

10+ Year Member



I dont usually use cookies/sessions a whole lot so I'm having a bit of a brain block with this and can't find a clear example to help me.

I have a small chunk of code on the home page of my site (a modal window...its not on any other page). I want to show this window only the first time a person hits the home page in a session.

Can anyone help me with this?

Thanks,
Chris

Matthew1980

8:01 pm on Mar 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there IntegrityWebDev,

Right, my first thought is to set a cookie and just sent it to something like FirstTime, then when they come back to the page, and isset $_COOKIE['firstTime'] and its equal to visited, dont show the box:-

setcookie("FirstTime", "visited", time()+60*60*24*30, "/");

then where you want the call:-

if(isset($_COOKIE['FirstTime']) && ($_COOKIE['FirstTime'] == "visited"))
{
//cookie set don't show the message
}else{
//Show the greeting!
}
I set the cookie there to 30 days, and the WHOLE domain "/" I find this a useful feature ;-p

Is that something like what you are after, there are many way's but that seems the most simplistic, though I suspect there will be a lot of suggestions!

Cheers,
MRb

IntegrityWebDev

8:40 pm on Mar 10, 2010 (gmt 0)

10+ Year Member



HA! That got it! I dont know why my brain kept telling me "that wont work". :-) Thanks Matthew!

Matthew1980

8:58 pm on Mar 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there IntegrityWebDev,

We all suffer from moments like that, just get the caffeine on! Glad that your sorted now.

Cheers,
MRb

Readie

9:02 pm on Mar 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Even if you tell yourself it won't work - it's no reason not to try. In fact, I consider it all the more reason to try, I swear about half the time I've told myself something won't work it turns out to be the right way to go about it :)