11Tami Posted January 11, 2008 Share Posted January 11, 2008 How do I get the cookie to remember which array it pulled from the first page load so it knows it should pull from the other array on the next page load? There are only two arrays included. Please let me know, thank you very much. <?php setcookie("a", $get1, time()+60*60*24*180); setcookie("b", $get2, time()+60*60*24*180); $one = Array(); $one[1] = "test1"; $one[2] = "test2"; $two = Array(); $two[1] = "test5"; $two[2] = "test6"; $getone = array_rand($one); $gettwo = array_rand($two); if(isset($_COOKIE['a']) && $getone){ echo $two[$gettwo];} if(isset($_COOKIE['b']) && $gettwo){ echo $one[$getone];} ?> Quote Link to comment https://forums.phpfreaks.com/topic/85545-solved-cookie-memory/ Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 Well rather than setting both cookies, you can set just one. And then when the user goes to another page, destroy that cookie and create a new one. That way, you can just pull out the string from whichever cookie is set and you would know which array to pull from. And so on and so forth. Quote Link to comment https://forums.phpfreaks.com/topic/85545-solved-cookie-memory/#findComment-436572 Share on other sites More sharing options...
11Tami Posted January 11, 2008 Author Share Posted January 11, 2008 You have been most helpful to me on this, I really appreciate it and will send you a special thank you. I'm trying this but the echo's still aren't appearing, anyone know why? Please let me know, thank you very much. <?php setcookie("a", $getone, time()+60*60*24*180); $one = Array(); $one[1] = "test1"; $one[2] = "test2"; $two = Array(); $two[1] = "test5"; $two[2] = "test6"; $getone = array_rand($one); $gettwo = array_rand($two); if(isset($_COOKIE['a'])){setcookie("b", $gettwo, time()+60*60*24*180); echo $two[$gettwo];} if(isset($_COOKIE['b'])){setcookie("a", $getone, time()+60*60*24*180); echo $one[$getone];} ?> Quote Link to comment https://forums.phpfreaks.com/topic/85545-solved-cookie-memory/#findComment-436581 Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 Well first, you don't need $getone as a variable in your cookie. You can tell well enough by the "a," right? Also, you need to destroy the set cookie once the new page is loaded. - Layout - First page: Destroy the cookie "b" if exists Set the cookie "a" Second page: Destroy the cookie "a" if exists Set cookie "b" Quote Link to comment https://forums.phpfreaks.com/topic/85545-solved-cookie-memory/#findComment-436591 Share on other sites More sharing options...
11Tami Posted January 11, 2008 Author Share Posted January 11, 2008 This works fine alone but when it goes into a web page or is included into a web page from another page with a php include, it quits working. It gives header errors. It doesn't seem to like new cookies set near the echos, which is a problem because the echos need to go in the body of the page so people can see their results. Does anyone know how I can set the cookies without getting the page header errors? Please let me know, thank you very much. <?php $one = Array(); $one[1] = "test1"; $one[2] = "test2"; $two = Array(); $two[5] = "test5"; $two[6] = "test6"; if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){ setcookie("array", "one", time()+60*60*24*180); echo $two[array_rand($two)]; }else{ setcookie("array", "two", time()+60*60*24*180); echo $one[array_rand($one)]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85545-solved-cookie-memory/#findComment-436623 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.