Create cookies by collecting cookies from a response object. The syntax format is:
Response.Cookies (Cookies名) [(key)|.属性] =值
Note: 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.
Cookies can be read out through the collection of cookies from the request object and delivered to the server side. The syntax format is:
Request.Cookies (Cookies名)
Among them, the cookie name is the name of the cookie that has been created.
Third, cookies properties
property name |
description |
domain |
|
expires |
cookies Expiration Date, in order to store cookies on the client disk after the session has ended, you must set the date. If the setting of this property does not exceed the current date, the cookie expires at the end of the task and writes only the property. |
path |
|
secure |
to set cookies Whether it is delivered in a secure channel. When a secure channel is not found on the HTTP home page, the cookie information will not be sent. |
haskeys |
designation cookies are Contains key keys, read-only properties. |
Using cookies to make a simple web counter, the form submits the page with the following code: (FileName: 4-3g.htm)
<body>
<center>
<form name="Form1" method="get" action="4-3g.asp">
<p>姓名:<input name="txtuserName" type="text">
<p>性别:<input name="optUserSex" type="radio" value="男">男
<input name="optUserSex" type="radio" value="女">女
<input Type="submit" name="btnSubmit" value="提交">
<input type="reset" name="reset" value="重写">
</center>
</form>
</body>
The code for the Process page (filename: 4-3g.asp) that counts how many times this page is accessed is as follows:
<% @ language= "VBScript"%>
<% response.buffer=true%>
<title> Read and write cookies example </title>
<body>
<center>
<%
N=request.cookies ("User") ("Visitedtimes")
Txtusername=request.querystring ("txtUserName")
Optusersex=request.querystring ("Optusersex")
If n= "" Then
N=1
Else
N=n+1
End If
%>
<font size= ' 7 ' color= ' Red ' face= ' Chinese cloud ' >
<%
If optusersex= "male" then
Response.Write txtUserName & "Sir, you are welcome to visit this site!" "
ElseIf optusersex= "female" then
Response.Write txtUserName & "Miss, you are welcome to visit this site!" "
End If
%>
</font>
<%
Response.Write "<P> This page is currently accessed <b>" & N & "</b> times. </p> "
Response.Cookies ("User") ("Visitedtimes") =n
Response.Cookies ("User"). Expires=date () +7 ' setting a cookie named user is valid for 7 days.
%>
<a href= "4-3g.htm" > Return to previous page </a>***| | <a href= "4-3-4.asp" > Return textbook </a>
</center>
</body>
Attention:
Usually cookies are stored in the C:windowscookies directory (for the win98/me system), and for the WIN2000/XP system, cookies are usually stored in c:documents and The cookies subdirectory in the user directory in the settings. If the client closes the cookie option, the session will not work. In the IE6.0, click the Inetnet Options menu item on the Tools menu, open the Intenet Options dialog box, select the Privacy tab, click the Advanced button in the Advanced Privacy Policy Settings dialog box, and select the Overwrite automatic cookies check box And then select the "Reject" option, and then the cookies are turned off.
See the full set of ASP Getting started tutorials