ASP Getting Started Tutorial-response object's Cookies collection

Source: Internet
Author: User
Tags file size header reset

A, cooies collection is a common collection of response objects and request objects. When a user accesses a home page through an HTTP header, it restarts each time. Therefore, if you want to determine whether a user has entered the site, then you can use cookies. When a user accesses this site for the first time, the data can be stored on the client computer using a collection of cookies from the response object, and when the user visits the site again, the Request object's cookie collection can be used to obtain relevant information.

Cookies are stored on the native disk of the browser, not on the server side. Typically, cookies contain information about the user (such as the identification number, password, how users shop on the Web site, or how many times users visit the site). Whenever a user connects to a server, the Web site can access the cookie information.

Note: What is an HTTP header? When the client browser sends a page request to the server, in addition to sending the URL address of the requested page to the server, the browser's type, version, and so on are sent to the server together, which is collectively called the request header; When the server responds to the client browser's request, in addition to the requested file being passed to the client , the file size, date and other information are sent to the client, which is called the response header, and the request header and response headers are collectively known as HTTP headers.

Second, the use of the response object of the cookie collection to store data to the client computer syntax format is as follows:

Response.Cookies(Cookies名)[(Key)]|.属性]=值

Annotations:

Key is an optional parameter, such as the specified key, then the cookie is a dictionary, the parameter value will be set to "value". And can be used in future reception. Index value is used to retrieve one of several variable values, and properties refer to the properties of the cookies. The cookie default lifecycle starts at the moment it is written to the browser end, and ends at the end of the browser's execution. If you want to set the lifetime of the cookise, you can use Response.Cookies (name). Expries property. Cookies are automatically removed from the browser's native disk when the expiration date is full. When writing cookie values to clients, it should be noted that because cookies are sent to clients as part of the HTTP transmission, the code that sets Cooie must precede the tag.

The following is a simple program that determines whether a user's username and password are entered correctly when they log on.

I created a cookie named username in the user login page and saved the username and user password with the two key values username and password. The code is as follows: (FileName: 4-2a1.asp)

<%
Response.Cookies("User")("UserName")="含笑" '创建一个名为"User"的Cookies中”UserName"的值。
Response.Cookies("User")("password")="111111" '创建一个名为“User"的Cookies中”password"的值。
Response.Cookies("UserName").Expires=date()+1
Response.Cookies("password").Expires=date()+1
%>
<body>
<center>
<form name="Form1" method="get" action="4-2a.asp">
<p>用户姓名:<input name="UserName1" type="text">
<p>密码:<input name="password1" type="password">&nbsp;&nbsp;
<input Type="submit" name="btnSubmit" value="提交">&nbsp;&nbsp;
<input type="reset" name="reset" value="重写">
</center>
</form>
</body>

Read the value in the cookie by the request object's cookie collection, and compare it with the username and password passed by the table conveys, if all the same, prompt: "You have logged in successfully." ", otherwise return to the login page. The code is as follows: (FileName: 4-2a2.asp)

<% @ language= "VBScript"%>
<% response.buffer=true%>
<title> Read and write cookies example </title>
<body>
<center>
<%
Dim username,password,a,b
Username=request.querystring ("UserName1") assigns the value of the named "UserName1" control to the variable UserName.
Password=request.querystring ("Password1") assigns the value of the named "Password1" control to the variable password.
A=request.cookies ("User") ("UserName") reads the value of UserName in the cookie and assigns it to variable a.
B=request.cookies ("User") ("password") reads the password value in the cookie and assigns it to variable B.
%>
<font size= ' 7 ' color= ' Red ' face= ' Chinese cloud ' >
<%
If Username=a and password=b then ' if the value entered in the UserName control is equal to the Uswrname value in the cookie, and the value entered in the password control equals the value of password in the cookie, Prompt "You are logged in successfully 1"
Response.Write "You have logged in successfully!" "
Else
Response. Redirect "4-2a1.asp" Otherwise, return to the User information input page.
End If
%>
</font>
<%
Response.Cookies ("User"). Expires=date () +100 ' Set the validity period of cookies is 100 days.
%>
<a href= "4-2a1.asp" > Return to previous page </a>***| | <a href= "4-2-2.asp" > Return textbook </a>
</center>
</body>

Note: After performing the above ASP program, you can find the written Cookie file on the client disk. The storage location of the Cookie file is related to the operating system you are using: If you are using Windows 98Me, you can find the file in the Windowscookies folder, and if you are using the W in200/xp system, cookies are stored in the C:D Ocuments in the Cookies subdirectory of the user directory in the ASN Settings.

See the full set of ASP Getting started tutorials

Related Article

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.