Cookies
Cookies in ASP
Β
Cookies are commonly used to identify users. They help store user-related information so that it can be accessed across multiple requests.
More Examples
Β
Welcome Cookie
This example demonstrates how to create a welcome cookie for a user.
What is a Cookie?
Β
A cookie is a small file stored on the userβs computer by the web server. Whenever the same browser requests a page from the server again, the stored cookie is sent back automatically.
Using ASP, you can both create cookies and read cookie values.
Creating a Cookie
Β
Cookies are created using the Response.Cookies object.
β οΈ Important:
The Response.Cookies statement must be written before the tag.
Example: Creating a Cookie
Β
<% Response.Cookies("firstname") = "Alex" %>
You can also set properties for a cookie, such as an expiration date:
Β
<% Response.Cookies("firstname") = "Alex" Response.Cookies("firstname").Expires = #May 10, 2012# %>
Retrieving a Cookie Value
Β
To read a cookie, use the Request.Cookies object.
Example:
Β
<% fname = Request.Cookies("firstname") Response.Write("Firstname=" & fname) %>
Output:
Β
Firstname=Alex
Cookies with Keys
Β
When a cookie stores multiple values, it is said to have keys.
Example: Creating a Cookie with Multiple Keys
Β
<% Response.Cookies("user")("firstname") = "John" Response.Cookies("user")("lastname") Β = "Smith" Response.Cookies("user")("country") Β = "Norway" Response.Cookies("user")("age") Β Β Β = "25" %>
Here, the cookie named user contains several related values.
Reading All Cookies
Β
Assume the server has sent the following cookies:
Β
<% Response.Cookies("firstname") = "Alex" Response.Cookies("user")("firstname") = "John" Response.Cookies("user")("lastname") Β = "Smith" Response.Cookies("user")("country") Β = "Norway" Response.Cookies("user")("age") Β Β Β Β = "25" %>
To read all cookies sent by the browser, including cookies with keys, use the following code:
Β
<% 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) & "
")
Β Β Next
Β Else
Β Β Response.Write(x & "=" & Request.Cookies(x) & "
")
Β End If
Β Response.Write("
Output:
Β
firstname=Alex user:firstname=John user:lastname=Smith user:country=Norway user:age=25
What If Cookies Are Not Supported?
Β
If a browser does not support cookies, alternative methods must be used to pass data between pages.
1. Passing Data Through URL Parameters
You can append values directly to the URL:
Β
Retrieve the values in welcome.asp:
Β
<% fname = Request.QueryString("fname") lname = Request.QueryString("lname") Response.Write("
Hello " & fname & " " & lname & "!
") Response.Write("Welcome to my Web site!
") %>Β
2. Passing Data Using a Form
You can also use an HTML form to send data:
Β
Retrieve the submitted values in welcome.asp:
Β
<% fname = Request.Form("fname") lname = Request.Form("lname") Response.Write("
Hello " & fname & " " & lname & "!
") Response.Write("Welcome to my Web site!
") %>