cookie problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gaurav_sting
Forum Newbie
Posts: 19
Joined: Sat Mar 27, 2004 3:45 am
Location: Delhi

cookie problem

Post by gaurav_sting »

hi everyone,

i am stuck in a problem, thanks for ur help. The problem is as follows:

first of all i want to know that can we store multiple values in a single cookie i.e. if we want to store multiple items' id (shopping cart) which the user adds to his/her cart. If yes then how, if no then is it possible to store an array in a cookie.

secondly, if it is possible to store multiple values in a single cookie say - the domainname and an array containing the item ids, and say the cookie name is cart, how can we associate and access these values (domainname and array elements) individually from the a single cookie.

Thirdly, i want to check whether the user's system has cookie set from our domain, how can i check when the user firsts opens/visits our site. Please help me also knowing that supposingly, the user visits the site and adds certain items to his/her cart, then a cookie is set, now when he visits the site again and adds more item to his/her cart, now is is possible to update that cookie with the new items selected or i will have to write another cookie.

If i have to write another cookie how can i destroy the previous cookie.

And finally, please tell the seqence of setting a cookie, do i have to start a session first then give a command setcookie?

Thanx alot for help
regards
Gaurav
Parin
Forum Newbie
Posts: 8
Joined: Thu Mar 25, 2004 7:36 am

Post by Parin »

first of all i want to know that can we store multiple values in a single cookie i.e. if we want to store multiple items' id (shopping cart) which the user adds to his/her cart. If yes then how, if no then is it possible to store an array in a cookie.
You can put an array into a cookie. But you have to serialize() the array first.

Code: Select all

$cookie = serialize( array($var1, $var2) );
set_cookie('cart', $cookie, time() + 31104000);

--to read
$cookie = stripslashes($_COOKIEї'cart']);
list($var1, $var2) = unserialize($cookie);
Thirdly, i want to check whether the user's system has cookie set from our domain, how can i check when the user firsts opens/visits our site. Please help me also knowing that supposingly, the user visits the site and adds certain items to his/her cart, then a cookie is set, now when he visits the site again and adds more item to his/her cart, now is is possible to update that cookie with the new items selected or i will have to write another cookie.
You can only read cookies from your own domain.
If a cookie exists, you find it through: $_COOKIE['cookiename'];
You cannot add to a cookie, you'll have to write it again. However, you can first read the existing values, put them in an array, add the new articles to the array and then set the cookie anew.
And finally, please tell the seqence of setting a cookie, do i have to start a session first then give a command setcookie?
You do need to start a session in order to use cookies. If you do use sessions however, you need to start it before you place the cookie.
Post Reply