User Specific Information using Cookie Attributes
What is a Cookie?
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer.
Each time the same computer requests a page with a browser, it will send the cookie too. With ASP, you can both 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 <html> tag. In the example below, 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 a cookie, like setting a date when the cookie should expire:
< %
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires=#May 10,2012#
%>
How to Retrieve a Cookie Value?
The "Request.Cookies" command is used to retrieve a cookie value.
In the example below, we retrieve the value of the cookie named "firstname" and display it on a page:
< %
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>
Cookie attributes
Name |
Description |
Domain |
Only send the cookie when requests are made (by the browser) to the specified domain. |
Expires |
This indicates the date on which the cookie expires. If you don't set this value, then the cookie is lost when the current session ends. |
HasKeys |
Does the cookie contain keys? |
Path |
If specified, the cookie is sent only to requests to this path (within the URL). If you don't specify a path, the application path is used. |
Secure |
Are the cookies secure? |