What is cookies

Source: Internet
Author: User

1. What is Cookies?

Cookie is a short text message that is transmitted between the Web server and the browser as user requests and pages. Cookie contains information that can be read by Web applications every time a user accesses the site.

For example, if the application sends a page to the user when the user requests the page in the site, it is not only a page, but also a Cookie containing the date and time, the user's browser obtains the Cookie while obtaining the page, and stores it in a folder on the user's hard disk.

Later, if the user requests the page on your site again, when the user enters the URL, the browser will find the Cookie associated with the URL on the local hard disk. If the Cookie exists, the browser sends the Cookie along with the page request to your site. Then, the application can determine the date and time of the user's last visit to the site. You can use this information to display a message to the user or check the expiration date.

Cookies are associated with websites rather than specific pages. Therefore, no matter which page on the site the user requests, the browser and server exchange Cookie information. When a user accesses different sites, each site may send a Cookie to the user's browser, which stores all the cookies separately.

Cookies help websites store information about visitors. In general, Cookie is a way to maintain the continuity of Web applications (that is, execution status management. Except for a short period of actual information exchange time, the browser and the Web server are disconnected. The Web server processes each request sent to the Web server separately. However, in many cases, it is very useful for Web servers to identify users on user request pages. For example, a Web server on a shopping site tracks each shopper so that the site can manage the shopping cart and other user-specific information. Therefore, Cookie can be used as a business card to provide relevant identification information to help the application determine how to continue execution.

Cookie can be used for multiple purposes, all of which are to help websites remember users. For example, a site that implements a public opinion test can simply use cookies as a Boolean value to indicate whether the user's browser has participated in the vote, so that the user cannot perform a second vote. Websites requiring users to log on can use cookies to record that users have logged on, so that users do not have to enter creden。 every time.

2. How to store Cookies

Cookies are stored on your local machine. Different browsers are stored in different folders and saved by domain name. That is, Cookies between websites are not covered by each other.

Users of IE can find the txt files of Cookies in local documents. windows xp files are stored in C: \ Documents and Settings \ Administrator \ Cookies folder. The name txt is saved by domain name. For example, the cookies in the 163 domain are: administrator@163%1%.txt%administrator@163%2%.txt.

3. How to transmit Cookies

Run the following example (asp.net simple addition, deletion, query, modification)

 

Public partial class Default5: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {} protected void button#click (object sender, EventArgs e) {HttpCookie cookie = new HttpCookie ("MyCook "); // initialize and set the Cookie name DateTime dt = DateTime. now; TimeSpan ts = new TimeSpan (0, 0, 1, 0, 0); // The expiration time is 1 minute cookie. expires = dt. add (ts); // set the cookie expiration time. values. add ("userid", "userid_value"); cookie. Values. add ("userid2", "userid2_value2"); Response. appendCookie (cookie); // output all content of the Cookie // Response. write (cookie. value); // The output is: userid = userid_value & userid2 = userid2_value2} protected void Button2_Click (object sender, EventArgs e) {// HttpCookie cokie = new HttpCookie ("MyCook "); // if (Request. cookies ["MyCook"]! = Null) {// Response. write ("the key value of Cookie is userid:" + Request. cookies ["MyCook"] ["userid"]); // The whole line // Response. write ("the key value of Cookie is userid2" + Request. cookies ["MyCook"] ["userid2"]); Response. write (Request. cookies ["MyCook"]. value); // output all values} protected void Button3_Click (object sender, EventArgs e) {// obtain the Cookie object HttpCookie cok = Request. cookies ["MyCook"]; if (cok! = Null) {// two methods for modifying a Cookie, cok. values ["userid"] = "alter-value"; cok. values. set ("userid", "alter-value"); // Add the new content cok to the Cookie. values. set ("newid", "newValue"); Response. appendCookie (cok) ;}} protected void Button4_Click (object sender, EventArgs e) {HttpCookie cok = Request. cookies ["MyCook"]; if (cok! = Null) {TimeSpan ts = new TimeSpan (-1, 0, 0, 0); cok. expires = DateTime. now. add (ts); // Delete the entire Cookie, as long as the expiration time is set to current Response. appendCookie (cok );}}}

Cookies are transmitted between the Web server and the browser. Save it in the Http request.

1. If the page requires writing Cookies, the returned Http is as follows. Note the bold part:

Response Header Information original header information cache-control privateconnection closecontent-length 820content-type text/html; charset = utf-8Date wed, 21 Nov 2012 14:57:28 gmtserver ASP. net Development Server/10.0.0.0set-Cookie mycook = userid = userid_value & userid2 = userid2_value2; expires = wed, 21-nov-2012 14:58:28 GMT; Path =/X-ASPnet-version 4.0.30319 request header information

2. When you request an HTTP header for a pageLocal CookiesAdd to HTTP Header

Original request header information: accept text/html, application/XHTML + XML, application/XML; q = 0.9, */*; q = 0.8accept-encoding gzip, deflateaccept-language ZH-CN, ZH; q = 0.8, en-US; q = 0.5, en; Q = 0.3 connection keep-alivecookie mycook = userid = userid_value & userid2 = userid2_value2host localhost: 1092 Referer http: // localhost: 1092/slwyweb/default5.aspxuser-Agent Mozilla/5.0 (Windows NT 5.1; RV: 17.0) Gecko/17.0 Firefox/17.0 request header information from the upload stream

4. How to view Cookies

(1) view the TXT file of cookies (the above example is stored in c: \ Documents ents and Settings \ Administrator \ cookiesadministrator@localhost%2%.txt)

The file contains the following content:

--------------------------------------------------

MyCook
Userid = userid_value & userid2 = userid2_value2
Localhost/
1024
3775538048
30263287
3177408048
30263287
*

----------------------------------------------------

(2) Using the plug-in (FF Web Developer plug-in) allows you to conveniently view, delete, and modify Cookies.

Cookies

 

1. Cookie restrictions

 

Most browsers support a Cookie of up to 4096 bytes.

 

The browser also limits the number of cookies that the site can store on the user's computer. Most browsers only allow 20 cookies to be stored on each site;Note that the 20 values here refer to the primary key value, that is, 20 Cookies. However, each cookie record can contain several subkeys, which will be explained in detail below. If you try to store more cookies, the oldest Cookie will be discarded.Some browsers impose absolute limits on the total number of cookies they will accept from all sites, typically 300.

2. Storage Format of Cookies

Cookies can contain a primary key, which then contains a subkey. For example, the format for obtaining Cookies in asp.net is:

Request.Cookies[key][subkey].ToString();

The key is the primary key, and the subkey is the subkey associated with the primary key.

(1) local disk storage format:

--------------------------------------

MyCook
Userid = userid_value & userid2 = userid2_value2
Localhost/
1024
3775538048
30263287
3177408048
30263287
*

------------------------------------

MyCook is the primary key; userid = userid_value & userid2 = userid2_value2 is the subkey associated with the primary key.

(2) Cookie format in Javascript

The Cookie in Javascript is a string obtained through document. cookies. The character format is as follows:

My.Common.SubKey=Pkid=999&TestValue=aaabbbcccdddeee; SingleKey=SingleKeyValue

The preceding string contains two Cookies. One is the SingleKey that does not contain the subkey, and the other is the My that contains the pkid and TextValue subkeys. common. subKey. The two cookies are separated.

(3) Cookies in Asp. Net

Through the Cookies of HttpResquest and HttpResponse objects, you can obtain and write Cookies on the current page. If the above already exists, I will not write it.

3. content encoding format of Cookies

The cookie value isYou can save punctuation marks other.However, Chinese characters cannot be saved. Garbled characters appear when saving Chinese characters.

Therefore, Cookies must be encoded and decoded in a unified manner. To enable decoding and encoding on both the browser and server, the UTF Encoding format must be used in a unified manner.

This is mainly because only UTF Encoding formats can be used in javascript.

4. Path attribute of Cookies

The Path attribute of Cookies indicates the Path in which the current Cookies can be used. Two Cookies with the same name but different paths are allowed.

The Cookies in the path on this page are obtained first on the server or client. (For example)

5. Cookie expiration time

If no expiration time is set when Cookies are saved, the expiration time of Cookies is "the current browser process is valid", that is, when the browser is closed like Session, it disappears.In asp.net, you can set the expiration time of the HttpCookie object to DateTime. MinValue to specify that the cookie takes effect with the browser. (This sentence is hard to come by. I found it with the help of my head and other people .)

If the expiration time is set and later than the current time, the cookie value is saved.

If the expiration time is set but less than or equal to the current time, the Cookies are cleared.

 

 

 

Thank you, ziqiu. zhang !!! Write such a good column

Want to know more about http://www.cnblogs.com/zhangziqiu/archive/2009/08/06/cookies-javascript-aspnet.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.