Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.