ASP getting started --- Cookie set of response object
This article comes from xuexiang Network (http://www.snowhack.com)
Address: http://www.snowhack.com/asp/aspjc/aspjc/200901/902.html
Xueyu: A combination of scripts and security, from entry to proficiency, creates the best programming learning site in China
1. The CooIes set is a commonly used set of response objects and request objects. When you access a home page through the HTTP header, you must start over each time. Therefore, if you want to determine whether a user has ever accessed this website, you can use cookies. When a user visits this website for the first time, the user can use the cookies set of the response object to store the data to the client computer. When the user visits this website again, you can use the cookies set of the request object to obtain relevant information.
Cookies are stored on the local disk of the browser, rather than on the server. Generally, cookies contain the user's information (such as the ID number, password, the user's way of shopping on the web site, or the number of times the user visits the site ). When a user connects to the server, the Web site can access cookies.
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, it also sends the browser type, version, and other information to the server, this information is collectively referred to as the request header. When the server responds to a request from the client browser, except that the requested file is transmitted to the client, the file size, date, and other information are also sent to the client. The information is called the response header, and the request header and response header are collectively referred to as the HTTP header.
2. Use the cookie set of the response object to store data on the client's computer. The syntax format is as follows:
Response. Cookies (cookies) [(key)] |. Attribute] = Value
Note:
Key is an optional parameter. If a key is specified, cookies are a dictionary. The value of this parameter is set to "value ". And can be used in future reception. "Index value" is used to retrieve one of multiple variable values. Attribute refers to the attribute of cookies. The default life cycle of cookies starts from the moment it is written to the browser, and ends when the browser ends. If you want to set the cookise validity period, you can use the response. Cookies (name). expries attribute. When the expiration time is reached, cookies are automatically deleted from the local disk of the browser. When writing cookie values to the client, note that because the cookie is sent to the client as part of HTTP transmissionCodeMust be placed before the tag.
3. The following section describes how to determine whether the user's username and password are correctly entered during logon. Program .
On the user logon page, I created a cookie named username. The two key values username and password are used to save the user name and password. The Code is as follows: (File Name: 4-2a1.asp) <%
Response. Cookies ("user") ("username") = "Smile" Create a value for "username" in cookies named "user.
Response. Cookies ("user") ("password") = "111111" 'create a value for "password" in cookies named "user.
Response. Cookies ("username"). expires = Date () + 1
Response. Cookies ("password"). expires = Date () + 1
%>
<HTML>
<Head> <title> User Profile </title>
<Body>
<Center>
<Form name = "form1" method = "get" Action = "4-2a.asp">
<P> User name: <input name = "username1" type = "text">
<P> password: <input name = "password1" type = "password"> & nbsp;
<Input type = "Submit" name = "btnsubmit" value = "Submit"> & nbsp;
<Input type = "reset" name = "reset" value = "Rewrite">
</Center>
</Form>
</Body>
</Head>
Use the cookie set of the request object to read the values in cookies and compare the user name and password passed through the form. If both are the same, the system prompts: "You have logged on successfully. ", Otherwise return to the logon page. The Code is as follows: (File Name: 4-2a2.asp)]
<% @ Language = "VBScript" %>
<% Response. Buffer = true %>
<HTML>
<Head>
<Title> Read and Write cookies </title>
</Head>
<Body>
<Center>
<%
Dim username, password, a, B
Username = request. querystring ("username1") 'assigns the value of the "username1" control to the variable username.
Password = request. querystring ("password1") 'assigns the value of the "password1" control to the variable password.
A = request. Cookies ("user") ("username") 'reads the username value in cookies and assigns it to variable.
B = request. Cookies ("user") ("password") 'reads the password value in cookies and assigns it to variable B.
%>
<Font size = '7' color = 'red' face = '文 '>
<%
If username = A and Password = B then', if the value entered in the username control is equal to the value of uswrname in cookies, and the value entered in the password control is equal to the value of password in cookies, the system prompts "you have logged on successfully 1".
Response. Write "you have logged on successfully! "
Else
Response. Redirect "4-2a1.asp" 'otherwise, return to the user information input page.
End if
%>
</Font>
<HR size = "2" width = "70%" color = "# cc9999">
<%
Response. Cookies ("user"). expires = Date () + 100 'sets the cookie validity period to 100 days.
%>
<A href = "4-2a1.asp"> return to the previous page </a> *** | *** <a href = "4-2-2.asp"> return to the Teaching Material </a>
</Center>
</Body>
</Html>
Note: After executing the 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 used. If Windows 98me is used, you can find the file in the Windows cookies folder, cookies are stored in the cookies subdirectory in the user directory in C: users' documents Asn settings.
This article comes from xuexiang Network (http://www.snowhack.com)
Address: http://www.snowhack.com/asp/aspjc/aspjc/200901/902.html
Xueyu: A combination of scripts and security, from entry to proficiency, creates the best programming learning site in China