1.11M Members

cookie array

 
0
 

i am trying to put all of my cookies into an array. heres my code

<?
setcookie('failn','fail'); setcookie('goodn','good'); setcookie('han','ha');

$allcookies=explode(' ',$_COOKIE);

print_r($allcookies);
?>

but the page only returns

Array ( [0] => Array )

please help

 
0
 

try

var_dump($_COOKIE);

if array is still empty then it means for some reason cookie didnt set.

 
0
 

is there any way to do it with explode?

 
0
 

$_COOKIE is already an array.

 
0
 

ok, but i want to split the array

 
0
 

The array is already split. What you're asking doesn't make any sense.

$_COOKIE is an array
e.g. array( key => value, key => value, key => value)

Explode is meant to take a string like "this is a test" and explode it into
array( 0 => this, 1 => is, 2 => a, 3 => test )

If you're trying to turn the array into a string, you want implode.

$string = implode( ' ', $_COOKIE );
//value value1 value2 value3 value4
 
0
 

now how would i do a for loop to search for a certain cookie, and then put that cookie value into a variable?

 
0
 

Can you give me a better example of what it is you have and what you're trying to find through a loop? Maybe some code that you're trying to get to work? setcookie('TestCookie', $value) would be read back with $_COOKIE

There isn't a reason to loop over the cookie array unless you don't know what the keys are already. If you don't know the keys you could iterate over the array in a variety of ways:

foreach( $_COOKIE as $key => $value ){
  if( $key == 'TestCookie' ){
    echo $value;
  }

  //OR
  if( $value == 'Some Random Value' ){
    echo $key;
  }

  //etc.
}
 
0
 

im just experimenting with different ways to get a php cookie into a variable

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: