Please visit our sponsors !
ASP Cookies
A cookie can be used to identify a user.
Examples
Welcome cookie
How to create a Welcome cookie.
What is a Cookie?
A cookie can be used to identify a user. It is a small file that the server
embeds on the user's computer. Each time the same computer asks for a
page through a browser, it will send the cookie
too.
With ASP, you can both create cookies and retrieve the value of cookies.
How to Create a Cookie
The "Response.Cookies" command is used to create a cookie:
<%
Response.Cookies("firstname")="Alex"
%>
|
In the code above, we have created a cookie named "firstname" and assigned
the value "Alex" to it.
Note: The Response.Cookies command must appear before the <html>
tag.
It is also possible to assign some properties to a cookie, like setting a
date when a cookie should expire:
<%
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.
How to Get a Cookie Value
The "Request.Cookies" command is used to get a cookie value.
In the example below, we retrieve the value of the cookie "firstname"
and display it on a page:
<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>
|
Output:Firstname=Alex
A Cookie with Keys
A cookie can also contain a collection of multiple values. We say that the
cookie has Keys.
In the example below, we will create a cookie-collection named
"user". The "user" cookie has Keys that contains information about a user:
<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>
|
Read all Cookies
The example below reads all the cookies your server has sent to a user. Note
that the code below checks if a cookie has Keys with the HasKeys property:
<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?
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:
1. Add parameters to a URL
Add parameters to a URL, like this:
<a href="greeting.asp?fname=John&lname=Smith">
Go to Welcome Page</a>
|
And retrieve the values like this (in the file greeting.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. Use a form
The form passes the filled out information to the file greeting.asp when the
user clicks on the Send button:
<form method="post" action="greeting.asp">
First Name: <input type="text" name="fname" value="">
Last Name: <input type="text" name="lname" value="">
<input type="submit" value="Send">
</form>
|
Retrieve the values like this (in the file greeting.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>")
%>
|
Jump to: Top of Page
or HOME or
Printer friendly page
Search W3Schools:
What Others Say About Us
Does the world know about us? Check out these places:
Dogpile
Alta Vista
MSN
Google
Excite
Lycos
Yahoo
Ask Jeeves
We Help You For Free. You Can Help Us!
W3Schools is for training only. We do not warrant its correctness or its fitness for use.
The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our
terms of use and
privacy policy.
Copyright 1999-2002 by Refsnes Data. All Rights Reserved
|