当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP > ASP Cookies
A cookie is often used to identify a user.
“Cookies”一般是用来确认用户信息的。
Welcome cookie
一个欢迎的cookie
How to create a Welcome 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”的值。
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”这个值赋予它:
<% |
It is also possible to assign properties to a cookie, like setting a date when the cookie should expire:
同时,你也可以给“Cookie”设置其他的属性,比如说,你可以给一段“Cookie”设置过期的时间:
<% |
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”的值,并将它在页面上显示出来:
<% |
Output:
输出:
Firstname=Alex
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“信息的”键”。
<% |
Look at the following code:
先请看以下这段代码:
<% |
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> <% for each x in Request.Cookies </body> |
Output:
输出结果:
firstname=Alex
user:firstname=John
user:lastname=Smith
user:country=Norway
user:age=25
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”,那又该怎么办呢?下面我们将介绍两种简单且行之有效的方法帮助你解决这个问题,帮你把你的请求从其中的一个页面发送到另外一个页面:
You can add parameters to a URL:
你可以在超链接的地址栏中加上参数:
<a href="welcome.asp?fname=John&lname=Smith"> |
And retrieve the values in the "welcome.asp" file like this:
然后,在“welcome.asp”文件当中获取“Cookie”的值,具体如下:
<% |
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"> |
Retrieve the values in the "welcome.asp" file like this:
通过以下的方法获取“welcome.asp”文件中的值:
<% |