PHP Page Redirect Based Upon Cookies

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
RobAC
Forum Newbie
Posts: 6
Joined: Mon Jan 22, 2007 11:59 am

PHP Page Redirect Based Upon Cookies

Post by RobAC »

I have a client that is running a short term promotion on their site. They want to have a splash page with the promotional information on it, but they only want the user to see the splash page once. If the user has already seen it, they want the user to go right to the regular home page of the site.

I was thinking that this can be done in PHP using a redirect based upon setting a cookie, but does anyone know how you could code this, or if there is better/more efficient way of doing this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It could be incorporated into the home page code itself, if you wanted, but the general idea is the same.
RobAC
Forum Newbie
Posts: 6
Joined: Mon Jan 22, 2007 11:59 am

Post by RobAC »

That's basically what I want to do, I'm just not sure how to embed the cookie checking/setting code into the PHP.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

isset(), $_COOKIE, setcookie() is really all you need to get started.
RobAC
Forum Newbie
Posts: 6
Joined: Mon Jan 22, 2007 11:59 am

Post by RobAC »

feyd wrote:isset(), $_COOKIE, setcookie() is really all you need to get started.
I need to do a series of if statements though that checks to see if the cookie is present. If it is not present, it sets the cookies and renders a chunk of HTML code.

If the cookies is already present, the user sees the rest of the page and not the promotional code.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Only one if should be required.
jammr
Forum Newbie
Posts: 16
Joined: Mon Jan 22, 2007 12:10 am

Post by jammr »

Just use something akin to the following:

Code: Select all

if(isset($_COOKIE['value'])) {
// present the page without setting the cookie
} else {
// set the cookie
}
RobAC
Forum Newbie
Posts: 6
Joined: Mon Jan 22, 2007 11:59 am

Post by RobAC »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Still can't get this to work properly.

At top of my index.php file, I have the following:

Code: Select all

<?php

if (!isset($_COOKIE['RafflePromo']))
    {
      setcookie("RafflePromo","1",time()+3600*24*365*30);
      header('Location: splash.php');
   }
?>
In the splash.php file I have:

Code: Select all

<html>
<head>
  <meta http-equiv="refresh" content="9;url=index.php" />
  <title>A Message from the New York State Lottery...</title>
    <link rel="stylesheet" type="text/css" media="screen" href="interstitial.css" />
</head>
<body>

<div align="center"><a href="index.php"><img src="testheader.jpg" width="749" height="54" border="0" alt="Raffles to Riches coming soon!"></a></div>

<p align="center"><img src="rafflepromo.jpg" width="500" height="500" border="2" alt="Raffles to Riches coming soon!"></p>

</body> 
</html>
The splash.php file just keeps refreshing in the browser every 9 seconds and the user never goes to index.php from there.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

try

Code: Select all

setcookie("RafflePromo","1",strtotime('+10 years'));
RobAC
Forum Newbie
Posts: 6
Joined: Mon Jan 22, 2007 11:59 am

Post by RobAC »

Kieran Huggins wrote:try

Code: Select all

setcookie("RafflePromo","1",strtotime('+10 years'));
didn't work.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

do you have cookies turned off? or some norton crap?
RobAC
Forum Newbie
Posts: 6
Joined: Mon Jan 22, 2007 11:59 am

Post by RobAC »

Kieran Huggins wrote:do you have cookies turned off? or some norton crap?
Yup. I just checked. I'm using IE 6. Went into Privacy and verified that it's accepting first and third party cookies. It's accepting cookies from other sites.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

also try with Firefox, and post the URL
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post Reply