ASP Cookie

Cookies are commonly used to identify users.

Example

Welcome cookie
How to create a welcome cookie.

What is a Cookie?

Cookies are commonly used to identify users. A cookie is a small file left on a user's computer by a server. Whenever the same computer requests a page through a browser, it also sends a cookie. With ASP, you can create and retrieve the value of a cookie.

How to create cookies?

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

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

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

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

It is also possible to assign properties to cookies, such as setting the expiration time of the cookie:

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

How to retrieve the value of cookies?

"Request.Cookies" command is used to retrieve the value of cookies.

In the following example, we retrieved the value of the cookie named "firstname" and displayed 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 can say that the cookie has keys (Keys).

In the following example, we will create a cookie collection named "user". The "user" cookie has keys containing user information:

<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Adams"
Response.Cookies("user")("country")="UK"
Response.Cookies("user")("age")="25"
%>

Read all cookies

Please read the following code:

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

Assuming your server has sent all these cookies to a user.

Now, we need to read these cookies. The following example shows you how to do this (note that the following code will use HasKeys to check if the cookie has keys):

<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=Adams
user:country=UK
user:age=25

How to deal with browsers that do not support cookies?

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

1. Add Parameters to URL

You can add parameters to the URL:

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

Then retrieve these values from a file similar to the following "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. Use Form

You can also use a form. When the user clicks the submit button, the form will submit the user's input data 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 from the "welcome.asp" file, like this:

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