954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Multiple values in one cookie

Hi All

I am a bit of a begginer in PHP.
How do I store multiple vales in a cookie?
I am going along the lines of:

$data = array('apple'=>'green',		'orange'=>'orange',
	'banana'=>'yellow');

then something like (not sure about the sytax here)::

$var=implode($data);
setcookie('data', $var, time() +3600);

Basically how do I get the information into and out of the cookie.

Thanks in advance for any help.

macfi
Newbie Poster
9 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Hi,

Why don't you concatenate values in a string like

apple,orange,mango
 and then store it in cookie.
vicky_rawat
Junior Poster
137 posts since Jun 2008
Reputation Points: 28
Solved Threads: 19
 

I really need it to be an associative array that is stored in the cookie and then be able to output the relevant data. If that makes any sense.

macfi
Newbie Poster
9 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Well if you want to implode the values and store it in the cookie then you'd explode it to retrieve it as an array.

$array = array('orange', 'green', 'blue');
$string = implode(',', $array); // 'orange,green,blue'
$second_array = explode(',', $string); // array('orange', 'green', 'blue');
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Thanks for all the help.
I know how to do the array part but I cannot get it into my head how to put the array into a single cookie and then get the data back out of the cookie.
I have spent most of yesterday googling for a solution with no real luck.

Thanks again in advance.

macfi
Newbie Poster
9 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Well you did it in your example. All I did was explain the correct syntax for implode/explode. Also, see www.php.net/cookies

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Thanks
but I am not sure on how to get the data back out of the cookie.

macfi
Newbie Poster
9 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 
Thanks but I am not sure on how to get the data back out of the cookie.


Then you didn't read the link I posted :)

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Hi
Thanks again for all the help, my head is going round and round as I have been at thsi for so long now. i right in saying to set the cookie I :

$data = array('apple'=>'green', 'orange'=>'orange',
'banana'=>'yellow');
$var=implode( ',', $data);
setcookie('data', $var, time() +3600);

and that has set the cookie?
I know how to set and then get everything out of a normal cookie
I alsoknw how to use the array but put them both together and I have a total block. I just cannot get my head round getting the data from within the array that is stored in the cookie. I have read your link but it is not helping me as I think my brain is fried.

Any help very very gratefully accepted.

macfi
Newbie Poster
9 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

If you know how to set and get data from the cookie I just gave you how to use implode/explode, just put them together. You already did the first part in your original post.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Hi
Thanks for the help I am nearly there but not quite.
I have set the cookie:

$data = array('apple'=>'green', 'orange'=>'orange',
'banana'=>'yellow');
$var=implode( ',', $data);
setcookie('fred', $var, time() +3600);


And to print it out I now have:

//print a cookie
$list=explode(',',$_COOKIE['fred']);
echo $list['0'];
echo $list['1'];
echo $list['2'];
echo '';
echo '';
echo '';
foreach ($list as $key => $value) {
echo "$key => $value ";
}

foreach ($list as $i =>$j)
{
echo "<p>An{$i} is {$j}. ";
}

This unfortunatley only gives me the value and not the key as below:

greenorangeyellow


0 => green
1 => orange
2 => yellow

An0 is green.

An1 is orange.

An2 is yellow.

How do I get it to print out the keys?
Thanks in advance

macfi
Newbie Poster
9 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

use serialize and unserialize instead of implode/explode

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Bother thought I was nearly there!

Now totally lost.
I have tried:

$data = array('apple'=>'green', 'orange'=>'orange',
'banana'=>'yellow');
$var=serialize($data);
print serialize($data);
$next=unserialize($var);
echo $next;


and I get
a:3:{s:5:"apple";s:5:"green";s:6:"orange";s:6:"orange";s:6:"banana";s:6:"yellow";}Array I got there

Thanks for anhep in advance

macfi
Newbie Poster
9 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

That is correct, you're not lost. You get

a:3:{s:5:"apple";s:5:"green";s:6:"orange";s:6:"orange";s:6:"banana";s:6:"yellow";}

from print serialize($data); and you get Array from echo $next

As a side note, don't mix echo and print, there's no reason to, they do the same thing.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Thanks again for all the help I really appreciate all the help I am getting, but now how do I get this into a cookie back out I seem to be tying myself up in knots!

macfi
Newbie Poster
9 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

heh, I guess I'm just not understand how you're not getting it. You can do 4 + 0 = 4 and you can do 0 + 4 = 4 but for some reason you can't do 2 + 2 = 4. You have the two pieces of the puzzle, you're just not putting them together.

Set the cookie

$some_array = array('Hello' => 'World');
setcookie('some_cookie', serialize($some_array), time() +3600);


Retrieve the cookie

$some_array = unserialize($_COOKIE['some_cookie']);
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Hi
Thanks for all your help I had few hours break went over it again and now have everything displaying properly both keys and value yipeee!

macfi
Newbie Poster
9 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You