当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP > ASP Cookies集合
The Cookies collection is used to set or get cookie values. If the cookie does not exist, it will be created, and take the value that is specified.
Cookies集合可用来设置或是获取cookie的值。如果cookie不存在,它就会被建立起来,并且会得到指定给它的值
Note: The Response.Cookies command must appear before the <html> tag.
注意:Response.Cookies命令必须出现在<html>标签之前
Response.Cookies(name)[(key)|.attribute]=value variablename=Request.Cookies(name)[(key)|.attribute] |
参数 | 描述 |
---|---|
name | Required. The name of the cookie 必选项。cookie的名称 |
value | Required for the Response.Cookies command. The value of the cookie 对于Response.Cookies命令来说是必选项。为cookie的值 |
attribute | Optional. Specifies information about the cookie. Can be one of the following parameters: 可选项。指定关于cookie的信息。可以是下面参数的一个:
|
key | 可选项. Specifies the key to where the value is assigned 将key指定到哪个被分配到的值 |
The "Response.Cookies" command is used to create a cookie or to set a cookie value:
"Response.Cookies"命令可用来建立或是设置cookie值:
<% |
In the code above, we have created a cookie named "firstname" and assigned the value "Alex" to it.
上面的代码建立了一个名为"firstname"的cookie,并且被赋予了"Alex"这个值
It is also possible to assign some attributes to a cookie, like setting a date when a cookie should expire:
也可以给cookie赋予一些属性,比如给cookie设置过期的时间:
<% |
Now the cookie named "firstname" has the value of "Alex", and it will expire from the user's computer at May 10, 2002.
现在这个名为"firstname",值为"Alex"的cookie将于2002年的5月10号从用户的计算机上过期。
The "Request.Cookies" command is used to get a cookie value.
"Request.Cookies" 命令可用来获得cookie值
In the example below, we retrieve the value of the cookie "firstname" and display it on a page:
下面这个举例,我们获取了cookie"firstname"的值,并将它显示在页面上:
<% |
Firstname=Alex
A cookie can also contain a collection of multiple values. We say that the cookie has Keys.
cookie还可以包含多个值。可以说cookie含有Keys
In the example below, we will create a cookie-collection named "user". The "user" cookie has Keys that contains information about a user:
下面这个举例,我们将会建立一个名为"user"的cookie-集合。"user"cookie含有包含关于用户的Keys:
<% |
The code below reads all the cookies your server has sent to a user. Note that the code checks if a cookie has Keys with the HasKeys property:
下面的代码会将服务器发送给用户的cookie读取出来。注意,代码使用了HasKeys属性来检查cookie是否含有Keys:
<html> <% for each x in Request.Cookies </body> |
输出:
firstname=Alex
user:firstname=John
user:lastname=Smith
user: country=Norway
user: age=25