Helpful Information
 
 
Category: Programming Languages
Only 20 Cookies??? - Please give me more cookies!

Hello. I am building a "custom links" feature on a web site. Currently, I am storing the url in a cookie and the name of the web site in another cookie. Having 10 custom links would mean 20 cookies. However, in the specifications of cookies (Netscape), there is a maximum limit of 20 on the number of cookies per domain. My question is: I know of a site at
http://www.hammervald.se/search-this/

which got around the problem by using a BIG cookie with all the information separated by a delimiter such as %. Would would be the code to parse this string in the cookie?

Suppose the contents of my cookie is:
CookieName = "URL1%site1.com%NAME1%site#1%URL2%site2.com%NAME2%site#2%URL3%site3.comNAME3%site#3..." How would I parse this string to get the following variables?

$URL1 = "site1.com"
$NAME1 = "site#1"... etc

Also, what would be a good delimiter to use? Is % very compatible?

Thank you for your help and I am very hungry for some more cookies http://www.devshed.com/Talk/Forums/smile.gif

Check out explode() and split() in the manual. As for a delimiter I wouldn't use % as that's used in url coding to delimit an encoded character. Pipe's (|) are good, usually.

However, you might want to use serialize()/unserialize() as an alternative. Store everything in a 2d array ($site[0][name], $site[0][url], etc) then $mycookie=serialize($site) and put $mycookie in the cookie. When you remove it just $site=unserialize($mycookie). Then you have no limits on cookies and only have to store one.

Thanks for the answer. However, I am confused about 2D arrays after reading the PHp documentation. I tried site[0][1] = $fav_name_1; but that did not work. So if I have the following variables stored already: $fav_path_1, $fav_name_1, $fav_path_2 and $fav_name_2, what would be the code to create a 2D array and to serialize/unserialize it? Should I worry about urlencoding the string? What about each() and list()? Should I care about them? Sorry about being such a newbie but any help would be greatly appreciated.

You forgot the $ before site[0][1].

After you have your array set up you can do this:

$data=serialize($site);
Then you can setcookie with $data.

After you read the cookie do:
$site=unserialize($data);

I don't use cookies much, but I don't think you need to urlencode them. Try it and find out. http://www.devshed.com/Talk/Forums/wink.gif

each and list can be used, if you want or you can just use the array. Up to you.

Thank you!

If I needed to track that much information about a user I'd probably store the urls and such in a database. That way you need only to store one or two cookies such as a username and session id. Which would relate directly back to the information in the DB.

This would give you great flexibility and allow you to store as many links as the user wants.










privacy (GDPR)