Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing multiple values in cookies

I have very large website which uses a lot of cookies. There are approx. 14 different cookies are there. I have different cookies for each item. When a user surfs the site they will have 14 cookies in their browser. I do not want this.

I want a single cookie for my site that will have 14 items and I can add,edit and delete them. I tried many ways but I am not able to do this.

I need to put some run time cookies as well save the user name in cookie. After the user logs in I want to save their personal site address in it. Eventually I want both the user name and personal site address both. I want to save user name before and then when user goes to his personal site then i will store personal site name run time.

Does any one have an idea how I could do this?

like image 749
Jalpesh Vadgama Avatar asked Jul 23 '09 18:07

Jalpesh Vadgama


People also ask

How many values can a cookie hold?

You do know that most browsers limit the amount of data you can store inside a single cookie? A site can have up to 20 cookies each of which can contain up to 4000 bytes of content. So the limit on the number of cookies is actually more restrictive than the amount that can be stored in one cookie in many cases.

Are cookies stored as name-value pairs?

A cookie is a piece of text that a web server can store on a user's hard disk. Cookies allow a web site to store information on a user's machine and later retrieve it. The pieces of information are stored as name-value pairs.

Can we store data in cookies?

Data stored in a cookie is created by the server upon your connection. This data is labeled with an ID unique to you and your computer. When the cookie is exchanged between your computer and the network server, the server reads the ID and knows what information to specifically serve to you.

How do I set multiple cookies at once?

With JavaScript, to set more than one cookie, set document. cookie more than once using the; separator.


1 Answers

Matthew beat me to it, but yes, see the ASP.NET Cookies Overview...

To write and read a single cookie with multiple key/values, it would look something like this:

HttpCookie cookie = new HttpCookie("mybigcookie"); cookie.Values.Add("name", name); cookie.Values.Add("address", address);  //get the values out string name = Request.Cookies["mybigcookie"]["name"]; string address = Request.Cookies["mybigcookie"]["address"]; 
like image 175
Kurt Schindler Avatar answered Oct 03 '22 09:10

Kurt Schindler