We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,839 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

cookie array

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

3
Contributors
8
Replies
7 Hours
Discussion Span
2 Years Ago
Last Updated
9
Views
tcollins412
Junior Poster
139 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0

try

var_dump($_COOKIE);

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

script99
Newbie Poster
2 posts since May 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

is there any way to do it with explode?

tcollins412
Junior Poster
139 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0

$_COOKIE is already an array.

mschroeder
Work Harder
Team Colleague
673 posts since Jul 2008
Reputation Points: 281
Solved Threads: 133
Skill Endorsements: 5

ok, but i want to split the array

tcollins412
Junior Poster
139 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 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
mschroeder
Work Harder
Team Colleague
673 posts since Jul 2008
Reputation Points: 281
Solved Threads: 133
Skill Endorsements: 5

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

tcollins412
Junior Poster
139 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 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.
}
mschroeder
Work Harder
Team Colleague
673 posts since Jul 2008
Reputation Points: 281
Solved Threads: 133
Skill Endorsements: 5

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

tcollins412
Junior Poster
139 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0818 seconds using 2.67MB