Paulto: Cookies

Source: Internet
Author: User

Cookies seem to have been forgotten in the age of the new technology Army. But the web design is still indispensable to its figure, here on my years of its application to do to summarize, may not be the most comprehensive, but it must be the most practical.

Cookies have long been not irreplaceable, not essential, and therefore lost a lot of color, many people describe it as a chicken ribs, food tasteless, discard. But if all of this is judged, it seems that many of our technologies are tasteless and seemingly indispensable. In my opinion, the best technology to do less contrast, more choices, more confusion, the heart is not quiet, to see what is useful and tasteless. But I think a lot of technology to use their own handy naturally have a taste.

Summary of the Cookie "food tasteless" there are probably several reasons.

(1). Unsafe, this is the most direct reason, because cookies are stored on the client computer, so it is easy to modify, resulting in access to the server when the execution of the application failed or other security issues.

(2). Serious restrictions on the size of the transfer content, which is one of the most important reasons, most browsers limit the size of the Cookie, in general, not more than 4,096 bytes, so the content of the transfer is limited.

(3). Users can control the opening and deactivation of cookies: this is the most frustrating reason, when the user set their browser to stop receiving cookies, so it will lose its role.

(4).

Although has been for the chicken ribs, but abandoned the always regrettable, the reason also is the following several original reasons.

(1). Simple to use: Because the use is too simple, so it can live to date, although there are a lot of people complaining every day, while secretly use it. Cookies are simple because it is the simplest text structure for index key values, and it is convenient to read and set data.

(2). Does not occupy any server resources: the generation of cookies is created by the server and entered with the Web page to the client, and resides on the client computer, so it does not need to occupy any resources on the server.

(3). The expiry date can be set at will: set the expiration date to make it quite useful, you can set the Cookie to disappear when the browser is finished, or set the expiry time of a feature, or even permanently save it on your computer.

(4).

OK, is a blessing is a curse, Rengeyouzhi, put aside. Here I have summed up the cookies in the web design in various aspects of the role and use of the method. Or that sentence, may not be the most complete, but it must be the most practical. Share with you. OK, here's the simplest use to start with.

1. What values can cookies store

Only personally identifiable information can be stored in cookies. Personally identifiable information refers to information that can be used to identify or contact a user. such as the user's name, e-mail, home address and so on. It must be emphasized that these identifiable information must be non-confidential or important information.

2. Save and read client information using a Cookie object.

To store a cookie variable, you can use the Response object's cookie collection by using the following syntax:

Response.Cookies[varName].Value=值;

Where VarName is the variable name.

To retrieve a cookie, you can use the collection of cookies for the Request object and return the specified cookie collection with the following syntax:

变量名=Request.Cookies[varName].Value;

For example, use cookies to manipulate client IP. The code is as follows:

使用Cookie保存和读取客户端信息
//保存客户端信息
string UserIP = Request.UserHostAddress.ToString();//获取客户端的IP地址
Response.Cookies["IP"].Value = UserIP;//将客户端的IP地址保存在Cookies对象中
Response.Cookies["IP"].Expires = DateTime.MaxValue;//设计Cookies的失效期
//读取
Response.Write(Request.Cookies["IP"].Value);//从Cookies中读取客户端IP地址值

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.