📄 asp_cookies.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ASP Cookies</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>ASP Cookies</h1>
<a href="asp_inputforms.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="asp_sessions.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">A cookie is often used to identify a user.</p>
<hr />
<h2>Examples </h2>
<p><a target="_blank" href="showasp.asp@filename=demo_cookies">Welcome cookie</a><br />
How to create a Welcome cookie.</p>
<hr />
<h2>What is a Cookie? </h2>
<p>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.</p>
<hr />
<h2>How to Create a Cookie?</h2>
<p>The "Response.Cookies" command is used to create cookies.</p>
<p><b>Note:</b> The Response.Cookies command must appear BEFORE the <html>
tag.
</p>
<p>In the example below, we will create a cookie named "firstname" and assign
the value "Alex" to it:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre><%
Response.Cookies("firstname")="Alex"
%></pre>
</td>
</tr>
</table>
<p>It is also possible to assign properties to a cookie, like setting a
date when the cookie should expire:
</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre><%
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires=#May 10,2002#
%></pre>
</td>
</tr>
</table>
<br />
<hr />
<h2>How to Retrieve a Cookie Value?</h2>
<p>The "Request.Cookies" command is used to retrieve a cookie value.
</p>
<p>In the example below, we retrieve the value of the cookie named "firstname"
and display it on a page:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre><%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%></pre>
</td>
</tr>
</table>
<br />
<b>Output:</b><p>Firstname=Alex</p>
<hr />
<h2>
A Cookie with Keys
</h2>
<p>If a cookie contains a collection of multiple values, we say that the
cookie has Keys.
</p>
<p>In the example below, we will create a cookie collection named
"user". The "user" cookie has Keys that contains information about a user:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre><%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%></pre>
</td>
</tr>
</table>
<br />
<hr />
<h2>Read all Cookies</h2>
<p>Look at the following code:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre><%
Response.Cookies("firstname")="Alex"
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%></pre>
</td>
</tr>
</table>
<p>Assume that your server has sent all the cookies above to a user.</p>
<p>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):</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre><html>
<body></pre>
<pre><%
dim x,y</pre>
<pre>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
%></pre>
<pre></body>
</html></pre>
</td>
</tr>
</table>
<p><b>Output:</b></p>
<p>firstname=Alex</p>
<p>user:firstname=John<br />
user:lastname=Smith<br />
user:country=Norway<br />
user:age=25</p>
<hr />
<h2>What if a Browser Does NOT Support Cookies?</h2>
<p>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:
</p>
<h3>1. Add parameters to a URL</h3>
<p>You can add parameters to a URL:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre><a href="welcome.asp?fname=John&lname=Smith">
Go to Welcome Page</a></pre>
</td>
</tr>
</table>
<p>And retrieve the values in the "welcome.asp" file like this:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre><%
fname=Request.querystring("fname")
lname=Request.querystring("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%></pre>
</td>
</tr>
</table>
<h3>2. Use a form</h3>
<p>You can use a form. The form passes the user input to "welcome.asp" when the
user clicks on the Submit button: </p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre><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></pre>
</td>
</tr>
</table>
<p>Retrieve the values in the "welcome.asp" file like this:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre><%
fname=Request.form("fname")
lname=Request.form("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%></pre>
</td>
</tr>
</table>
<br />
<hr />
<a href="asp_inputforms.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="asp_sessions.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -