iarp Posted March 8, 2009 Share Posted March 8, 2009 <?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 More sharing options...
iarp Posted March 8, 2009 Author Share Posted March 8, 2009 Oh i made a mistake. I changed the cookies name in between getting ready to post this. It's now called 'header_img'. I've changed all the iarp_header_img values. Link to comment https://forums.phpfreaks.com/topic/148438-solved-setcookie-not-changing-value/#findComment-779311 Share on other sites More sharing options...
corbin Posted March 8, 2009 Share Posted March 8, 2009 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. Link to comment https://forums.phpfreaks.com/topic/148438-solved-setcookie-not-changing-value/#findComment-779318 Share on other sites More sharing options...
iarp Posted March 8, 2009 Author Share Posted March 8, 2009 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. Link to comment https://forums.phpfreaks.com/topic/148438-solved-setcookie-not-changing-value/#findComment-779319 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.