当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP > ASP Cookies集合

ASP
ASP Drive
ASP File
ASP Folder
ASP Dictionary
ASP ADO
ASP AdRotator
ASP BrowserCap
ASP Content Linking
ASP Content Rotator
ASP Quick Ref
ASP 摘要
ASP 实例

ASP Cookies集合


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 460 ::
收藏到网摘: n/a

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的信息。可以是下面参数的一个:
  • Domain -  只写. cookie 只发送到请求的这个域
  • Expires - 只写. cookie过期的日期. 如果日期没有指定,当Session结束时Cookie也自动过期
  • HasKeys - 只读. 指定的cookie是否有keys (这个属性只能用在 Request.Cookies 命令上)
  • Path - 只写. If set, the cookie is sent only to requests to this path. If not set, the application path is used
    如果设置,那么cookie只有当这个路径请求的时候才会发送。不设置,就使用适用的路径
  • Secure - 只写. 如果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值:

<%
Response.Cookies("firstname")="Alex"
%>

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设置过期的时间:

<%
Response.Cookies("firstname")="Alex" 
Response.Cookies("firstname").Expires=#May 10,2002#
%>

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"的值,并将它显示在页面上:

<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>

输出:

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:

<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>

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>
<body>
<%
dim x,y
for each x in Request.Cookies
response.write("<p>")
if Request.Cookies(x).HasKeys then
for each y in Request.Cookies(x)
response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
response.write("<br /")
next
else
Response.Write(x & "=" & Request.Cookies(x) & "<br />")
end if
response.write "</p>"
next
%>
</body>
</html>
%>

输出:

firstname=Alex

user:firstname=John
user:lastname=Smith
user: country=Norway
user: age=25

评论 (0) All

登陆 | 还没注册?