Jump to content

[SOLVED] setcookie() not changing value


iarp

Recommended Posts

<?php
if(!isset($_COOKIE['iarp_header_img'])) { 
	setcookie("iarp_header_img", '1', time()+60*60*24*30,"/",".iarp.ca");
	$url = $_SERVER['REQUEST_URI'];
	header("Location: $url");
}
						echo "Before: " . $_COOKIE['iarp_header_img'];

switch ($_COOKIE['iarp_header_img']) {
	case '1':
		setcookie("iarp_header_img", '2', time()+60*60*24*30,"/",".iarp.ca");
		break;
	case '2';
		setcookie("iarp_header_img", '1', time()+60*60*24*30,"/",".iarp.ca");
		break;
}
													echo "After: " . $_COOKIE['iarp_header_img'];

?>

 

further down the page:

 

							<?php 
						switch ($_COOKIE['header_img']) {
							case '1':
								echo '<img src="/themes/gear/images/ico.big/earth.png" alt="Earth"></img>';
								break;
							case '2';
								echo '<img src="/themes/gear/images/ico.big/gear.png" alt="Gear"></img>';
								break;
						}
						?>

 

The image in the second block of code on this post, won't change the image, it stays as 1 all the time.

 

I don't need to reload the page in the first switch case because i'm setting the value to the next # for whenever someone changes the page yet again. Any helps appreciated.

Link to comment
https://forums.phpfreaks.com/topic/148438-solved-setcookie-not-changing-value/
Share on other sites

If I remember correctly, setcookie does not alter the value $_COOKIE.

 

 

(Assuming I remember correctly:)

 

 

setcookie's only responsability is to send the correct header for sending a cookie.  It would be a design flaw, in my opinion for setcookie to alter $_COOKIE since $_COOKIE is data read back from the client's cookie, and simply calling setcookie does not mean that the client will receive/honor a cookie header.

	echo "Before: " . $_COOKIE['header_img'];

switch ($_COOKIE['header_img']) {
	case '1':
		$_COOKIE["header_img"] = '2';
		break;
	case '2';
		$_COOKIE["header_img"] = '1';
		break;
}

echo "After: " . $_COOKIE['header_img'];

 

After reading that, i made a few changes. This time the Before and After echo have the appropriate values but are not keeping the values with each page change.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.