PDA

View Full Version : Redirect cookie


Meltdown
11-27-2004, 09:12 PM
I have this code <?php

if (!empty($_GET['choice']))
{
setcookie("mozunk.com", $_GET['choice'], time()+(60*60*24*365), "/", ".mozunk.com", 1);
}

if (!empty($_COOKIE['mozunk.com']))
{

header("Location: " . $_COOKIE['mozunk.coml']);
}
else
{
/*continue with your entrance script, then use a post/url
method to set the cookie later. You can send it back to this page again*/
}


?> which I got from a friend. It's supposed to allow a user to see an alternative index page the first time he/she visits the site and then the next time he/she visits the site, he/she sees the regular index. Kindof like deviantart did for version 2. However, I can't seem to get it working right and any help would be appreciated. The alternative index is called "welcome.html" and the regular index is called "index.php" Thanks for any help :D

marek_mar
11-27-2004, 09:24 PM
It will redirect to welcome.html when the cookie mozunk.com does not exist.

<?
if(!isset($_COOKIE['mozunk.com']))
{
setcookie("mozunk.com", $_GET['choice'], time()+(60*60*24*365), "/", ".mozunk.com", 1);
header("Location: welcome.html");
die();
}
?>

Scrowler
11-27-2004, 09:27 PM
header("Location: " . $_COOKIE['mozunk.coml']);

in this situation, cookie "mozunk.coml" doesnt exist, so you will always be refferred to the wrong place

im not sure but possibly in this situation, the header may not change location at all if there is no location specified. change it and see what happens.

Meltdown
11-27-2004, 09:29 PM
that didn't work..it sticks on welcome.html

Fou-Lu
11-28-2004, 03:48 AM
252226


Do you have more in that script, or is that everything?
I gave that code specific that it would have a link back to itself with a get added to it, though it can be done in many different ways.

Scrowler
11-28-2004, 05:06 AM
just out of interest... what's going to be stored in this cookie?

--edit-- server configuration might prevent you from using cookies...

Fou-Lu
11-28-2004, 09:32 AM
Thats the first that I've heard of servers affecting cookies.
Cookies are client side, so your telling the browser to set its preference, which it will store on its harddrive. All the php is doing is 1. Asking permission to set the cookie and 2. if its okay to access and make use of the cookie.
The only configuration that can change this feature is using variables_order. Which of course is an easy fix.

Now, the problem with your code is this. Set cookie doesn't affect a php script until the load. So if your using the code you have exactly as its stated, with nothing else, it will never work.

Part of your problem is relating to the mozunk.coml when searching for the location. This cookie is not / never will be set. You have in no way asked it to set itself.
What you need to do is in the section with add your own get/post data, you need to send it back to PHP_SELF. Add a choice to it, and you can use a post/get method if you want. If you have more than one page, or want to limit what is usable by choice, you can add an array and function to evaluate that choice is an available option.
Lots can be done with this, but in order to set the cookie, you will need to relate it back to itself if you use the code I gave.