A cookie is usually a small piece of data sent from a website to a visitor while the visitor is browsing a website. When the user browses the same website in the future, the data stored in the cookie can be retrieved by the website to be used for processes related to the previous activity from the user.
Cookies were designed to be used as a mechanism for websites to retain information about the activity the visitor had taken in the past. This is especially common with sites that have shopping carts. Shopping cart information can be stored in a cookie so that when the user returns to the site in the future, the unprocessed shopping cart items would still be available to the user for further processing.
As with ASP Sessions, ASP Cookies are used to store information specific to a user of your website. Cookies are stored on the user’s computer during the life of the session or for an extended period of time. If you set the expiration date of the cookie for some day in the future it will remain there until that day unless manually deleted by the user.
ASP Create Cookies
Creating an ASP cookie is very similar to the process of creating an ASP Session variable. As with the Session variable, you must create a name|key/value pair where the key will be the name of the cookie. The cookie will store the value which contains the actual data. The Response.Cookies
command is used to create cookies. The Response.Cookies
is placed before the opening <html>
tag.
Syntax
<%
Response.Cookies(name)[(key)|.property]=value
%>
Parameter | Description |
---|---|
name | Name of the cookie |
key | Optional parameter which specifies a key for the value to be assigned |
property | Optional parameter to specify information about the cookie (see below) |
value | The value of the cookie |
Properties
Property | Description |
---|---|
Domain | [Write-only] The cookie is sent only to requests to this domain |
Expires | [Write-only] Instructs the browser to delete the cookie at the specified date. If no date is specified, the cookie will expire when the session ends |
HasKeys | [Read-only] Specifies whether the cookie has keys |
Path | [Write-only] If set, the cookie is sent only to requests to this path. If not set, the application path is used |
Secure | [Write-only] If set to True then the cookie will only be sent to the server when using a secure connection |
In the following example, we will create a cookie named userid
and assign the value of john1
to it. The cookie will be set to expire 1 year from the date that it was saved to the user’s computer. When the cookie expires it is deleted from the user’s computer.
<%
Response.Cookies("userid")="john1"
Response.Cookies("userid").Expires = Date + 365
%>
In this example, 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")("age")="30"
%>
As you may have noted above, there are a few properties that you can set using cookies. The first property is the Domain
property. This is the domain that the cookie originated from. Fortunately, the cookie can only be read by the domain it originated from. It is set by default to the domain in which it was created, but you can modify it according to your needs.
<%
Response.Cookies("name").Domain = "www.domainame.com"
%>
The next property in the list is the Expires
property. This specifies the date the cookie should expire. If it is not set or set to a past date then it will expire when the browser is closed. The date can be set using different methods. You can use the current date and add or subtract days or set a specific date.
<%
Response.Cookies("name").Expires = Date + 365
Response.Cookies("name").Expires = #January 01, 2015#
%>
The HasKeys
property is Read-Only
and is used to retrieve cookie information. It is not used when setting specific properties. However, the next item, the Path
property is important when using cookies. This specifies in more detail the exact path on the domain that can use the cookie. For example, this would set the path that can retrieve the cookie info.
<%
Response.Cookies("name").Path = "/cookies/"
%>
The last property on the list is the Secure
property. If set, the cookie will only be set if the browser is using secure sockets (https) to connect.
<%
Response.Cookies("name").Secure = True
%>
ASP Retrieve Cookies
The easiest way to retrieve a cookie is to do so by using the Request.Cookies
command.
<%
firstName=Request.Cookies("firstName")
Response.Write("Firstname=" & firstName)
%>
If you want to read all the cookies, you can do so using a loop and conditional statement along with the HasKeys
property.
<!DOCTYPE html>
<html>
<head>
<title>Server Variables</title>
<style type="text/css">
table {
width:50%;
margin:20px auto;
border:1px solid gray;
border-collapse:collapse;
}
tr, th, td {
border:1px solid gray;
padding:10px
}
</style>
</head>
<body>
<%
dim cookie, keys
Response.Write("<table>")
Response.Write("<tr><th>Name</th><th>Key</th><th>Value</th></tr>")
For Each cookie In Request.Cookies
If Request.Cookies(cookie).HasKeys Then
For Each keys In Request.Cookies(cookie)
Response.Write("<tr><td>" & cookie & "</td><td>" & keys & "</td><td>" & Request.Cookies(cookie)(keys) & "</td></tr>")
Next
Else
Response.Write("<tr><td>" & cookie & "</td><td> </td><td>" & Request.Cookies(cookie) & "</td></tr>")
End If
Next
Response.Write("</table>")
%>
</body>
</html>