Training ASP Tutorial: Cookies


What is a Cookie ?


Well , a cookie is used for identification of  a user.
A file is embedded by the server on the user’s machine.
ASP helps in creating and retrieving cookies.


Creation of a Cookie
In ASP training, a command “Response.Cookies” is used to do so.
It must be used as a precursor to the <html> tag.


Let us see an example :
<%  Response.Cookies("firstname")="ASP tutorial" %>


This code snippet illustrates creation of a cookie and assigns the value “ASP tutorial” to it.

Now, a cookie can not reside on the user’s machine forever.
For this,we can set a date when a cookie expires :


<%

Response.Cookies("firstname")="ASP tutorial" 

Response.Cookies("firstname").Expires=#March 10,2008#

%>

 
    
 

Retrieval of a Cookie
In ASP , a command “Request.Cookies” is used to do so.


Let us see an example :

<%

fname=Request.Cookies("firstname")

response.write("Firstname=" & fname)

%>

 

This code snippet illustrates retrieval of a cookie named “firstname” and displays the value on the page.


 

A Cookie with Keys
We say that the cookie has Keys, when it contains multiple values, 


In the following code snippet, a cookie collection named "admin" is created. The "admin" cookie has Keys containing information :

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


asp tutorials