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

ASP
ASP 介绍
ASP Install
ASP 语法
ASP 变量
ASP Procedures
ASP Forms
ASP Cookies
ASP Session
ASP Application
ASP #include
ASP Global.asa
ASP Send e-mail
ASP Response
ASP Request
ASP Application
ASP Session
ASP Server
ASP Error
ASP FileSystem
ASP TextStream

ASP Cookies


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

A cookie is often used to identify a user.
“Cookies”一般是用来确认用户信息的。


Examples 举例

Welcome cookie
一个欢迎的cookie
How to create a Welcome cookie.
如何建立一个欢迎的cookie


 

What is a Cookie?什么是“Cookie”?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With ASP, you can both create and retrieve cookie values.
“Cookie”通常是用来确认用户信息的。当你访问某个站点时,服务器会会把一小段信息发送到你的电脑上,这小段信息就是“Cookie”。每次,当你用同一台电脑上的浏览器访问页面时,“cookie”也会同时被发送到你的浏览器上。你可以通过ASP来创建或者获取“Cookie”的值。


How to Create a Cookie?
如何创建一个“Cookie”?

The "Response.Cookies" command is used to create cookies.
“Response.Cookies”是用来创建“Cookie”的指令。

Note: The Response.Cookies command must appear BEFORE the <html> tag.
提示:“Response.Cookies”必须在<html>标签之前使用。

In the example below, we will create a cookie named "firstname" and assign the value "Alex" to it:
在下面的案例当中,我们将创建一个名为“firstname”的“Cookie”,并将“Alex”这个值赋予它:

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

%>

It is also possible to assign properties to a cookie, like setting a date when the cookie should expire:
同时,你也可以给“Cookie”设置其他的属性,比如说,你可以给一段“Cookie”设置过期的时间:

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

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

 


How to Retrieve a Cookie Value?
如何获取一个“Cookies”的值?

The "Request.Cookies" command is used to retrieve a cookie value.
“Request.Cookies”是用获取“Cookies”值的指令。

In the example below, we retrieve the value of the cookie named "firstname" and display it on a page:
在下面的例子当中,我们将通过“Request.Cookies”的指令获取名为“firstname”的“Cookie”的值,并将它在页面上显示出来:

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


Output:
输出:

Firstname=Alex


A Cookie with Keys
带有多个键(关键词)的“Cookies”

If a  cookie contains a collection of multiple values, we say that the cookie has Keys.
如果一个“Cookie”是一个带有多个值的集合,那么我们就称这个“Cookies”带有多个“键”(也称为“带有字典的Cookie”)

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“将同时带有多个包含这个”user“信息的”键”。

<%
Response.Cookies("user")("firstname")="John"

Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"

%>

 


Read all Cookies
读取所有“Cookie”的值

Look at the following code:
先请看以下这段代码:

<%
Response.Cookies("firstname")="Alex"
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"

Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>

Assume that your server has sent all the cookies above to a user.
假设你的服务器已经把以上所有的“Cookie”值发给你的用户了

Now we want to read all the cookies sent to a user. The example below shows how to do it (note that the code below checks if a cookie has Keys with the HasKeys property):
现在我们想把发送到用户手里的所有“Cookie”值读取出来。下面的案例将告诉我们如何实现这个功能(这段代码先是确认这段“Cookie”是否带有“键”,然后再读取里面的信息的)。

<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>

Output:
输出结果:

firstname=Alex

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


What if a Browser Does NOT Support Cookies?
如果浏览器不支持“Cookies”该怎么办?

If your application deals with browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application. There are two ways of doing this:
如果对方的浏览器不支持“Cookies”,那又该怎么办呢?下面我们将介绍两种简单且行之有效的方法帮助你解决这个问题,帮你把你的请求从其中的一个页面发送到另外一个页面:

1. Add parameters to a URL
1、 在超链接的地址栏中加上参数:

You can add parameters to a URL:
你可以在超链接的地址栏中加上参数:

<a href="welcome.asp?fname=John&lname=Smith">
Go to Welcome Page</a>

And retrieve the values in the "welcome.asp" file like this:
然后,在“welcome.asp”文件当中获取“Cookie”的值,具体如下:

<%
fname=Request.querystring("fname")
lname=Request.querystring("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%>

2. Use a form
2、 使用表单:

You can use a form. The form passes the user input to "welcome.asp" when the user clicks on the Submit button: 
你可以使用表单来解决这个问题。当用户点击“提交”按钮后,表单将把用户输入的信息发送到“welcome.asp”文件。

<form method="post" action="welcome.asp">
First Name: <input type="text" name="fname" value="">
Last Name: <input type="text" name="lname" value="">

<input type="submit" value="Submit">
</form>

Retrieve the values in the "welcome.asp" file like this:
通过以下的方法获取“welcome.asp”文件中的值:

<%
fname=Request.form("fname")
lname=Request.form("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%>

评论 (0) All

登陆 | 还没注册?