Jump to content

[SOLVED] cookie memory


11Tami

Recommended Posts

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];}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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];}
?>

 

     

 

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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)];
}
?>

Link to comment
Share on other sites

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.