![]() |
|
|
| Subject:
Setting a Cookie in PHP
Category: Computers > Programming Asked by: jackking5750-ga List Price: $15.00 |
Posted:
12 Mar 2006 16:34 PST
Expires: 13 Mar 2006 11:04 PST Question ID: 706522 |
I have a PHP script that sends a user to a homepage called index.php
then redirects them to one of six random homepages. I using this
script to test the effectiveness of different homepages.
Now I want to modify the script, so each user will see the same
version of the homepage they saw when they first visited if they visit
again. From a experience perspective I don't want a returning user to
see a different homepage or they might think they've visited the wrong
site.
I'm assuming that I should set a cookie to do this and place the
cookie code before the code that chooses a random homepage. The
randomizer selects a homepage with a value of 0 to 5, so I figure the
cookie should record this value for each user upon their first visit
to the site. The cookie would override the randomizer if a user visits
the site again.
For example, if a user visits the site they might be redirected to
index5.php. That would mean that the randomizer chose randomtopic 4
which corresponds to index5.php. That user should be redirected to
index5.php as the homepage if they visit again.
Please add the necessary code to the script below. I am not a
programmer, so please show me and explain exactly what I need to do to
get this working.
Thanks!
<?php
srand((double)microtime()*1000000);
$randomtopic = rand(0,5);
if ($randomtopic == "0") {
header('Location: index1.php);
} else if ($randomtopic == "1") {
header('Location: index2.php);
} else if ($randomtopic == "2") {
header('Location: index3.php);
} else if ($randomtopic == "3") {
header('Location: index4.php);
} else if ($randomtopic == "4") {
header('Location: index5.php);
} else if ($randomtopic == "5") {
header('Location: index6.php);
} else {
echo ( "ERROR: There was a problem running the script. ");
}
?> |
|
| There is no answer at this time. |
|
| Subject:
Re: Setting a Cookie in PHP
From: happydrgn-ga on 12 Mar 2006 19:42 PST |
<?php
/**
* Chooses a random page and saves a cookie
*
* returns $randomtopic
* -OR- FALSE on error
*/
function rand_page() {
srand((double)microtime()*1000000);
$randomtopic = rand(1,6);
if(setcookie("MyCookie", $randomtopic)) {
return $randomtopic;
} else {
return FALSE;
};
};
/**
* Checks if the cookie is set
*
* returns $page_num
* -OR- ERROR -AND- Returns random $page_num
*/
if(isset($_COOKIE['MyCookie'])) {
$page_num = $_COOKIE['MyCookie'];
} else {
if(!$page_num = rand_page()) {
echo ("ERROR: There was a problem saving the cookie.\n</ BR>");
$page_num = rand(1,6);
};
};
$page_name = 'index'.$page_num.'.php';
header('Location: '.$page_name);
?> |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |