ASP Cookies


Cookies are commonly used to identify users.


Examples

Try it Yourself - Examples

Welcome cookie
This example demonstrates how to create a Welcome cookie.


What is a Cookie?

Cookies are commonly used to identify users. A cookie is a small file that the server leaves on the user's computer. Whenever the same computer requests a page through a browser, this computer will send the cookie. With ASP, you can create and retrieve the value of cookies.


How to Create a Cookie?

The "Response.Cookies" command is used to create cookies.

Note:The Response.Cookies command must appear before the <html> tag.

In the example below, we will create a cookie named "firstname" and assign it the value "Alex":

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

It is also possible to assign properties to a cookie, such as setting the cookie's 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 the value of cookies.

In the example below, we retrieve the value of the cookie named "firstname" and display the value 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 can say that the cookie has keys.

In the example below, we will 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"
%>


Read 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"
%>

Assume that your server has sent all the above cookies to a user.

Now, we need to read all the cookies that were sent to a user. The example below demonstrates how to do this (note that the code below checks whether the cookie has keys through the HasKeys property):

<!DOCTYPE html>
<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 the Browser Does Not Support Cookies?

If your application needs to deal with browsers that do not support cookies, then you have to use other methods to pass information between pages in your application. Here are two methods:

1. Add parameters to the URL

You can add parameters to the URL:

<a href="welcome.asp.html">Go to Welcome Page</a>

Then retrieve these values in the "welcome.asp" file, as shown below:

<%
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

You can use a form. When the user clicks the Submit button, the form passes the user input to "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>

Then retrieve these values in the "welcome.asp" file, as shown below:

<%
fname=Request.form("fname")
lname=Request.form("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%>

Other Extensions