ASP Cookies
Cookies are commonly used to identify users.
Try it - Example
Welcome cookie
This example demonstrates how to create a Welcome cookie.
What is a Cookie?
Cookies are often used to identify users. A cookie is a small file that a server leaves on a user's computer. Each time the same computer requests a page through a browser, it sends the cookie. With ASP, you can create and retrieve cookie values.
How to Create a Cookie?
The "Response.Cookies" command is used to create cookies.
**Note:** The Response.Cookies command must appear before the tag.
In the example below, we create a cookie named "firstname" and assign it the value "Alex":
<%
Response.Cookies("firstname")="Alex"
%>
It is also possible to assign attributes to a cookie, such as setting its expiration time:
<%
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires=#May 10,2012#
%>
How to Retrieve the Value of a Cookie?
The "Request.Cookies" command is used to retrieve cookie values.
In the example below, we retrieve the value of the cookie named "firstname" and display it on the page:
<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>
Output: Firstname=Alex
Cookies with Keys
If a cookie contains a collection of multiple values, we say the cookie has keys (Keys).
In the example below, we create a cookie collection named "user". The "user" cookie has keys that contain user information:
<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>
Reading All Cookies
Please read the code below:
<%
Response.Cookies("firstname")="Alex"
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>
Suppose your server sent all the cookies above to a user.
Now we need to read all those cookies sent to a user. The example below shows you how to do this (note that the code below uses the HasKeys property to check whether a cookie has keys):
<%
dim x,y
for each x in Request.Cookies
response.write("
")
if Request.Cookies(x).HasKeys then
for each y in Request.Cookies(x)
response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
response.write("
")
next
else
Response.Write(x & "=" & Request.Cookies(x) & "
")
end if
response.write "
"next
%>
Output:
firstname=Alex
user:firstname=John
user:lastname=Smith
user:country=Norway
user:age=25
What if the Browser Does Not Support Cookies?
If your application must deal with browsers that do not support cookies, you have to use other methods to pass information between the pages of your application. Here are two methods:
1. Add parameters to the URL
You can add parameters to the URL:
Then retrieve these values in the "welcome.asp" file, as shown below:
<%
fname=Request.querystring("fname")
lname=Request.querystring("lname")
response.write("
Hello " & fname & " " & lname & "!
")response.write("
Welcome to my Web site!
")%>
2. Use a form
You can use a form. When the user clicks the Submit button, the form passes the user input to "welcome.asp":
Then retrieve these values in the "welcome.asp" file, as shown below:
<%
fname=Request.form("fname")
lname=Request.form("lname")
response.write("
Hello " & fname & " " & lname & "!
")response.write("
Welcome to my Web site!
")%>