Google Answers Logo
View Question
 
Q: Setting a Cookie in PHP ( No Answer,   1 Comment )
Question  
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. ");   
}   
?>
Answer  
There is no answer at this time.

Comments  
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);  
?>

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

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 Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy