asp初级教程:ASP Cookies

ASP Cookies

Cookie是经常被用来识别用户。

范例

<%
dim numvisits
response.cookies("NumVisits").Expires=date+365
numvisits=request.cookies("NumVisits")

if numvisits="" then
   response.cookies("NumVisits")=1
   response.write("Welcome! This is the first time you are visiting this Web page.")
else
   response.cookies("NumVisits")=numvisits+1
   response.write("You have visited this ")
   response.write("Web page " & numvisits)
   if numvisits=1 then
     response.write " time before!"
   else
     response.write " times before!"
   end if
end if
%>
<html>
<body>
</body>
</html>
输出结果.

Welcome! This is the first time you are visiting this Web page.

cookie是什么?
Cookie是经常被用来识别用户。 Cookie是一个小型的档案服务器嵌入在用户的计算机上。每次在同一台计算机请求一个网页的浏览器,它将把饼干太多。与ASP ,你都可以创造和检索的cookie值。

-------------------------------------------------- ------------------------------

如何创建一个Cookie ?
该“ Response.Cookies ”命令用于创建的Cookie 。

注: Response.Cookies命令必须出庭<html>标记。

在下面的例子中,我们将创建一个cookie命名为“名字” ,并指定值“亚历克斯” ,以它:

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

也有可能的cookie ,如确定一个日期时,应到期的cookie :

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

 

如何撷取一个cookie价值吗?
该“ Request.Cookies ”命令用于检索一个cookie值。

在下面的例子中,我们撷取的价值Cookie的名为“名字” ,并显示在网页上:

 

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

 

输出结果.

 

Firstname=Alex

 

一个Cookie的钥匙
如果一个cookie包含了收集的多重价值,我们说的cookie了钥匙。

在下面的例子中,我们将创建一个Cookie收集命名为“用户” 。 “用户的” cookie了钥匙,其中包含的信息用户:

 

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

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

假设您的服务器已寄出所有的cookies以上的用户。

现在,我们要阅读所有的Cookie发送给用户。下面的例子显示了如何做到这一点

(请注意,下面的代码检查是否有一个Cookie键与HasKeys财产) :

<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

 

如果浏览器不支持cookies ?
如果您的申请涉及的浏览器不支持cookies ,您将不得不使用其他方法来传递信息从一个网页到另一个在您的申请。有两种方法这样做的:

1 。新增参数以一个网址
您可以添加参数的网址

 

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

和检索的价值观在“ welcome.asp ”文件像这样:

 

<%
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 。使用形式
您可以使用的一种形式。形式传递给用户输入“ 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>
 

取回的价值在“ 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>")
%>

转载请注明来自www.111cn.net/asp/asp.html

 

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索cookie
, 网页
, 服务器
, 浏览器
, 参数
用户
cookie、cookies设置、cookies在哪、cookie是什么意思、js cookies,以便于您获取更多的相关知识。

时间: 2024-08-31 13:14:46

asp初级教程:ASP Cookies的相关文章

asp入门教程:ASP Session 对象简介

asp入门教程:ASP Session 对象简介 Session对象是用来储存的信息,或更改设置的一个用户会议.变量存储在Session对象举办资讯单一用户,并提供给所有的网页在一个应用程序. -------------------------------------------------- ------------------------------ Session对象 当你正与一个应用程序,你打开它,做一些改变,然后将其关闭.这是很像会议.计算机知道你是谁.它知道当您启动应用程序,当您结束

asp fso教程:ASP Delete 方法

asp fso教程:ASP Delete 方法 删除的方法删除指定的文件或文件夹. 语法 FileObject.Delete[(force)] FolderObject.Delete[(force)] Parameter Description force 任择.一个布尔值,表明是否有只读文件或文件夹都将被删除.真指出,只读文件/文件夹将被删除和虚假表示,它不会被删除.默认是虚假的. 来一个delete 对文件的操作实例. <% dim fs,f set fs=Server.CreateObje

asp入门教程:ASP FileSystemObject 对象

asp入门教程:ASP FileSystemObject 对象 使用FileSystemObject对象是用来存取档案系统的服务器上. 下面我们来举例说明吧. <html> <body> <% Set fs=Server.CreateObject("Scripting.FileSystemObject") If (fs.FileExists("c:winntcursors3dgarro.cur"))=true Then       Re

asp入门教程:ASP applocation 全局变量与 Global.asa 文件

asp入门教程:ASP 全局变量 Global.asa 文件 Global.asa文件是一个可选文件,可以包含申报的对象,变量和方法,可以通过在每一页的ASP应用程序. -------------------------------------------------- ------------------------------ Global.asa文件 Global.asa文件是一个可选文件,可以包含申报的对象,变量和方法,可以通过在每一页的ASP应用程序.所有有效的浏览器的脚本( Java

asp入门教程:ASP Server 对象简单

asp入门教程:ASP Server 对象简单 在ASP服务器对象用于访问属性和方法在服务器上. 好了下面我们来看一个实例: <html> <body> <% Set fs = Server.CreateObject("Scripting.FileSystemObject") Set rs = fs.GetFile(Server.MapPath("demo_lastmodified.asp")) modified = rs.DateLa

asp入门教程:ASP Request 简单介绍

asp入门教程:ASP Request 简单介绍,在ASP Request对象是用来获取信息的用户.它有两种方法form 与querystring的, 下面我们来看看request.from 这里是专门用于表单如. <form name=a action=? > <input name=b value='this is request.form' /> </form> asp代码如下. <% response.write request.from("b&

asp入门教程:ASP Response 对象教程

asp入门教程:ASP Response 对象教程 ASP Response对象是用来发送输出到用户从服务器. 下面我们来看一个简单的例子吧. <% response.write " i love asp " %> 输出的结果为 i love asp response的作用是输出数据.的. 转载请注明来自http://www.111cn.net/asp/asp.html

ASP入门教程-ASP和脚本语言

什么是 ASP ASP 称活动服务器页面(英文全称Active Server Pages)就是一个编程环境,在其中,可以混合使用HTML.脚本语言以及组件来创建服务器端功能强大的Internet应用程序. 如果您以前创建过一个站点,其中混合了HTML.脚本语言以及组件,您就可以在其中加入ASP程序代码.并且,还可以通过使用组件包含一些商业逻辑规则.组件可以被脚本程序调用,也可以由其他的组件调用. ASP的工作原理: 当在Web站点中融入ASP功能后,将发生以下事情: 1.用户调出站点内容,默认页

asp fso教程:asp move文件移动实例教程

move移动方法移至指定的文件或文件夹从一个位置到另一个位置. 语法 FileObject.Move(destination) FolderObject.Move(destination) Parameter Description destination Required. Where to move the file or folder. Wildcard characters are not allowed 对文件操作简单例子. <% dim fs,f set fs=Server.Crea