Page 1 of 1
PHP Page Redirect Based Upon Cookies
Posted: Mon Jan 22, 2007 12:02 pm
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?
Posted: Mon Jan 22, 2007 12:08 pm
by feyd
It could be incorporated into the home page code itself, if you wanted, but the general idea is the same.
Posted: Mon Jan 22, 2007 2:37 pm
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.
Posted: Mon Jan 22, 2007 2:41 pm
by feyd
isset(), $_COOKIE,
setcookie() is really all you need to get started.
Posted: Mon Jan 22, 2007 2:48 pm
by RobAC
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.
Posted: Mon Jan 22, 2007 2:53 pm
by feyd
Only one if should be required.
Posted: Mon Jan 22, 2007 4:01 pm
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
}
Posted: Tue Jan 23, 2007 1:50 pm
by RobAC
feyd | Please use 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
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]
Posted: Tue Jan 23, 2007 1:54 pm
by Kieran Huggins
try
Code: Select all
setcookie("RafflePromo","1",strtotime('+10 years'));
Posted: Tue Jan 23, 2007 2:12 pm
by RobAC
Kieran Huggins wrote:try
Code: Select all
setcookie("RafflePromo","1",strtotime('+10 years'));
didn't work.
Posted: Tue Jan 23, 2007 2:13 pm
by Kieran Huggins
do you have cookies turned off? or some norton crap?
Posted: Tue Jan 23, 2007 2:20 pm
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.
Posted: Tue Jan 23, 2007 2:21 pm
by Kieran Huggins
also try with Firefox, and post the URL
Posted: Tue Jan 23, 2007 9:53 pm
by feyd