cookies - redirecting to alternating home pages

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
dirksmith
Forum Newbie
Posts: 11
Joined: Fri Oct 07, 2005 4:19 pm

cookies - redirecting to alternating home pages

Post by dirksmith »

Hi

I'd like to set-up a simple cookie system that redirects the user to 1 of 2 home pages - so the home page effectively alternates.

I'm a bit of a php newbie - does anyone know who this is done and what the script would look like?

Thanx

Dirk
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

so you mean you want the home page to be x the first time they visit and they y the next time, then back to x etc etc?

if so, just create a page with a require_once in it.

check if the cookie is set, if so, check it's value and use a conditional to alternate between the "included" pages, then reset the cookie.

using this scenario would change the home page if they hit a sub page and go back though. You could incorporate sessions as well to prevent that.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: cookies - redirecting to alternating home pages

Post by RobertGonzalez »

dirksmith wrote:Hi

I'd like to set-up a simple cookie system that redirects the user to 1 of 2 home pages - so the home page effectively alternates.

I'm a bit of a php newbie - does anyone know who this is done and what the script would look like?

Thanx

Dirk
Something along the lines of ...

Code: Select all

if (isset($_COOKIE['mycookiechecker']))
{
    header("Location: http://www.mysite.com/page1.php");
}
else
{
    header("Location: http://www.mysite.com/page2.php");
}
4rum
Forum Newbie
Posts: 1
Joined: Fri Nov 11, 2005 7:33 am

CHECK FOR COOKIE EXISTENCE

Post by 4rum »

This seems to be troubling me also...

All I need to do is check for the existence of a cookie, and then if necessary grab the value and then do what needs doing. I don't want to use session for this.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

if ( isset($_COOKIE['cookie_name']) )
{
    $mycookie = $_COOKIE['cookie_name'];
    // Do what you want to do if the cookie exists
}
This is the simplest way. Of course you can always set this comparison to check the value of the cookie var for even greater security.
Post Reply