asp: Add, modify, delete

Source: Internet
Author: User

Cookies, sometimes in their plural form, refer to the data (usually encrypted) stored on the user's local terminal by certain websites in order to identify the user's identity. Defined in RFC2109. It was the invention of Netscape's former employee Lou Montulli in March 1993.

The server can use cookies to contain information that is arbitrary to filter and regularly maintain this information in order to determine the status in the HTTP transmission. The most typical application of cookies is to determine whether a registered user has logged on to the site, and users may be prompted whether to retain user information for the next time they enter the site in order to simplify the login process, which is the function of cookies. Another important application is the "shopping cart" process. Users may select different items on different pages of the same site for a period of time, which will be written to cookies to extract information at the end of the payment.

Cookies can keep the login information to the user's next session with the server, in other words, the next time you visit the same website, the user will find that you do not have to enter a user name and password to log in (of course, do not exclude the user manually delete cookies). There are also some cookies that are deleted when the user exits the session, which can effectively protect the privacy of the individual.

   //Write    protected voidButton1_Click (Objectsender, EventArgs e) {HttpCookie Cookie=NewHttpCookie ("Mycook");//Initial and set the name of the cookieDateTime dt=DateTime.Now; TimeSpan TS=NewTimeSpan (0,0,1,0,0);//expiration time is 1 minutesCookies. Expires = dt. ADD (TS);//Set Expiration TimeCookies. Values.add ("userid","Userid_value"); Cookies. Values.add ("Userid2","userid2_value2");    Response.appendcookie (cookie); //output All content of this cookie//Response.Write (Cookies. Value);//output is: userid=userid_value&userid2=userid2_value2}//Read    protected voidButton2_Click (Objectsender, EventArgs e) {//HttpCookie Cokie = new HttpCookie ("Mycook");//the first order of the      if(request.cookies["Mycook"]!=NULL) {//Response.Write ("The value of the key in the cookie is userid:" + request.cookies["Mycook" ["userid"]);//entire Row//Response.Write ("Value of Userid2 in cookie" + request.cookies["Mycook" ["Userid2"]);Response.Write (request.cookies["Mycook"]. Value);//Output All Values}}//Modify Cookies    protected voidButton3_Click (Objectsender, EventArgs e) {//Gets the cookie object for the clientHttpCookie Cok = request.cookies["Mycook"]; if(Cok! =NULL) {//two ways to modify cookiesCok. values["userid"] ="Alter-value"; Cok. Values.set ("userid","Alter-value"); //Add new content to cookiesCok. Values.set ("newid","NewValue");    Response.appendcookie (COK); }}//Delete Cookies    protected voidButton4_Click (Objectsender, EventArgs e) {HttpCookie Cok= request.cookies["Mycook"]; if(Cok! =NULL) {if(!checkbox1.checked) {cok. Values.remove ("userid");//remove a value with a key value of UserID}Else{TimeSpan TS=NewTimeSpan (-1,0,0,0); Cok. Expires= DATETIME.NOW.ADD (TS);//Delete the entire cookie, just set the expiration time to now} response.appendcookie (COK); }}

asp: Add, modify, delete

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.