Set a Cookie
Moderator: General Moderators
Set a Cookie
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?
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
1. don't do it =)
2. you can use session too
2. you can use session too
Code: Select all
session_start();
$_SESSION['visited'] = true;
//check if visited is set
Re: Set a Cookie
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.
Use setcookie() to achieve what you are looking for.
Re: Set a Cookie
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
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 
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Set a Cookie
But the problem with a cookie, is that it will expire. Do sessions last forever if you don't destroy them?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Set a Cookie
No, sessions don't last forever. And to make it more complicated, many times the session uses a cookie.
(#10850)
Re: Set a Cookie
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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Set a Cookie
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
there is one problem:
What if user has cookies disabled? than hi would see the popup every time he refreshes the page, very annoying
What if user has cookies disabled? than hi would see the popup every time he refreshes the page, very annoying
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Set a Cookie
Use sessions as a fallback.. that was they will only get one popup per visit.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
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
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
storing ip would be the best one, there is "problem" with dynamic ip's but better than nothing
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Set a Cookie
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.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
(#10850)
Re: Set a Cookie
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 ...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.
The option here is to check if cookies are enabled by javascript, that would do it
Re: Set a Cookie
Wow! Thanks for the discussion.
So it looks like I should use the session. Still not sure how, though.
So it looks like I should use the session. Still not sure how, though.