Set a Cookie

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
cupaball
Forum Commoner
Posts: 85
Joined: Sun Feb 12, 2006 1:46 pm

Set a Cookie

Post 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?
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: Set a Cookie

Post 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
 
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Re: Set a Cookie

Post 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.
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: Set a Cookie

Post 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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Re: Set a Cookie

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Set a Cookie

Post by Jonah Bron »

But the problem with a cookie, is that it will expire. Do sessions last forever if you don't destroy them?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Set a Cookie

Post by Christopher »

No, sessions don't last forever. And to make it more complicated, many times the session uses a cookie.
(#10850)
cupaball
Forum Commoner
Posts: 85
Joined: Sun Feb 12, 2006 1:46 pm

Re: Set a Cookie

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Set a Cookie

Post 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
}
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: Set a Cookie

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Set a Cookie

Post 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.
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: Set a Cookie

Post 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:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Set a Cookie

Post 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. ;)
(#10850)
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: Set a Cookie

Post 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
cupaball
Forum Commoner
Posts: 85
Joined: Sun Feb 12, 2006 1:46 pm

Re: Set a Cookie

Post by cupaball »

Wow! Thanks for the discussion.

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