Page 1 of 1

Set a Cookie

Posted: Sat Jan 19, 2008 10:01 am
by cupaball
Hi All:

I am in the process of re-designing my site and wanted to create a light box effect that showed a message like " welcome to the new site blah, blah, blah.." and the would click a button to close, but I only want it to show the first time the user visits the home page. I was assuming I could use a cookie and with some sort of if statement. Can someone help me with this?

Re: Set a Cookie

Posted: Sat Jan 19, 2008 10:55 am
by arjan.top
1. don't do it =)

2. you can use session too

Code: Select all

 
session_start();
 
$_SESSION['visited'] = true;
 
//check if visited is set
 

Re: Set a Cookie

Posted: Sat Jan 19, 2008 2:11 pm
by Oren
He asked for a cookie, not a session. Although the session id might be kept in a cookie, this is not what the original poster asked for and the session data may not be available the next time the user visits the page.
Use setcookie() to achieve what you are looking for.

Re: Set a Cookie

Posted: Sat Jan 19, 2008 3:32 pm
by arjan.top
I understood that he wants to show message on the first view at any time, not just once for the whole time

Re: Set a Cookie

Posted: Sat Jan 19, 2008 3:54 pm
by Oren
Well actually... after reading his post again I'm not sure myself what he wants exactly, so maybe what you suggested is what he wants and maybe it's what I did... who knows. We'll have to wait to the original poster's reply :P

Re: Set a Cookie

Posted: Sat Jan 19, 2008 4:24 pm
by Jonah Bron
But the problem with a cookie, is that it will expire. Do sessions last forever if you don't destroy them?

Re: Set a Cookie

Posted: Sat Jan 19, 2008 4:33 pm
by Christopher
No, sessions don't last forever. And to make it more complicated, many times the session uses a cookie.

Re: Set a Cookie

Posted: Sat Jan 19, 2008 6:40 pm
by cupaball
Thanks for the help.

What I am looking for is way to have message pop up only the first time a user visits the home page. So for example, if they visit the services page and the return to the home page they would not get the message again.

Re: Set a Cookie

Posted: Sat Jan 19, 2008 7:19 pm
by John Cartwright
Like already suggested, set a cookie using setcookie() and then check for the cookies existance using

Code: Select all

 
if (!isset($_COOKIE['my_cookie_name_here'])) {
   //display popup
}

Re: Set a Cookie

Posted: Sun Jan 20, 2008 4:19 am
by arjan.top
there is one problem:
What if user has cookies disabled? than hi would see the popup every time he refreshes the page, very annoying

Re: Set a Cookie

Posted: Sun Jan 20, 2008 9:34 am
by John Cartwright
arjan.top wrote:there is one problem:
What if user has cookies disabled? than hi would see the popup every time he refreshes the page, very annoying
Use sessions as a fallback.. that was they will only get one popup per visit.

Heck.. you could record their ip and check against that as well. But like the other methods, it is not completely accurate.

Re: Set a Cookie

Posted: Sun Jan 20, 2008 10:17 am
by arjan.top
but sessions store id in cookie so that won't work (you can have id in url but that is ugly etc.)

storing ip would be the best one, there is "problem" with dynamic ip's but better than nothing :lol:

Re: Set a Cookie

Posted: Sun Jan 20, 2008 12:52 pm
by Christopher
arjan.top wrote:but sessions store id in cookie so that won't work (you can have id in url but that is ugly etc.)

storing ip would be the best one, there is "problem" with dynamic ip's but better than nothing :lol:
Ok ... now you have pretty well thought through the issues with identifying a unique user just like the PHP group did many years ago. Now just use sessions. ;)

Re: Set a Cookie

Posted: Sun Jan 20, 2008 1:23 pm
by arjan.top
arborint wrote: Ok ... now you have pretty well thought through the issues with identifying a unique user just like the PHP group did many years ago. Now just use sessions. ;)
wold you like alert message (or something like that) every time you refresh the page because you don't have cookies enabled? This is he trying to do ...

The option here is to check if cookies are enabled by javascript, that would do it :D

Re: Set a Cookie

Posted: Mon Jan 21, 2008 5:51 pm
by cupaball
Wow! Thanks for the discussion.

So it looks like I should use the session. Still not sure how, though.