943,776 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 4935
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 16th, 2009
0

Multiple values in one cookie

Expand Post »
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:

php Syntax (Toggle Plain Text)
  1. $data = array('apple'=>'green', 'orange'=>'orange',
  2. 'banana'=>'yellow');


then something like (not sure about the sytax here)::
php Syntax (Toggle Plain Text)
  1. $var=implode($data);
  2. setcookie('data', $var, time() +3600);


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

Thanks in advance for any help.
Last edited by peter_budo; Apr 16th, 2009 at 8:03 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
macfi is offline Offline
9 posts
since Apr 2009
Apr 16th, 2009
0

Re: Multiple values in one cookie

Hi,

Why don't you concatenate values in a string like
PHP Syntax (Toggle Plain Text)
  1. apple,orange,mango
  2. and then store it in cookie.
Reputation Points: 28
Solved Threads: 19
Junior Poster
vicky_rawat is offline Offline
137 posts
since Jun 2008
Apr 16th, 2009
0

Re: Multiple values in one cookie

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
macfi is offline Offline
9 posts
since Apr 2009
Apr 16th, 2009
0

Re: Multiple values in one cookie

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.

php Syntax (Toggle Plain Text)
  1. $array = array('orange', 'green', 'blue');
  2. $string = implode(',', $array); // 'orange,green,blue'
  3. $second_array = explode(',', $string); // array('orange', 'green', 'blue');
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Apr 16th, 2009
0

Re: Multiple values in one cookie

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
macfi is offline Offline
9 posts
since Apr 2009
Apr 16th, 2009
0

Re: Multiple values in one cookie

Well you did it in your example. All I did was explain the correct syntax for implode/explode. Also, see www.php.net/cookies
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Apr 16th, 2009
0

Re: Multiple values in one cookie

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

Re: Multiple values in one cookie

Click to Expand / Collapse  Quote originally posted by macfi ...
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
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Apr 16th, 2009
0

Re: Multiple values in one cookie

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 :
php Syntax (Toggle Plain Text)
  1. $data = array('apple'=>'green', 'orange'=>'orange',
  2. 'banana'=>'yellow');
  3. $var=implode( ',', $data);
  4. 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.
Last edited by peter_budo; Apr 16th, 2009 at 8:03 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
macfi is offline Offline
9 posts
since Apr 2009
Apr 16th, 2009
0

Re: Multiple values in one cookie

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.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Parsing error on non existant line
Next Thread in PHP Forum Timeline: Parse error: syntax error, unexpected $end





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC