Custom Shopping Cart & eCommerce Merchant Account Integration services

 

Request a Free Quote

Contact Us - Call 1-877-627-2492    


 
Earth Skater Custom eCommerce [Home]
  Authorize.net Certified Developer PayPal Certified Integration CyberSource Certified Developer
      You are here:   Home · Dev Zone · ASP Cookies Tutorial      
 
eCommerce Hosting
Affiliate Software
Website Analytics
Merchant Accounts
Merchant Integration

Internet Marketing
Content Management
Custom Development
Custom Web Design

Why Earth Skater
Our Clients
Our Partners
     
 
 

Cookies in ASP

A cookie is a small piece of information that a server can store in a Web browser. Each time the browser requests a page from the server it sends the relevant cookies too. ASP allows you to create and retrieve cookies easily.

Creating a Cookie

Cookies are created using Response.Cookies. Cookies are sent in the headers of the response so you should always set cookies before sending the body of the response.

Example Cookie Creation

<%
Response.Cookies("username") = "earthskater"
Response.Cookies("username").Expires = Date + 365
%>

In the example above a cookie named "username" was created with a value of "earthskater". Then the expiry date was set using the Expires property. The cookie was set to expire 1 year from the days date. When the cookie expires it is deleted from the user's Web computer.

Retrieving Cookies

To retrieve cookies we use Request.Cookies.

Example Cookie Retrieval

<%
username = Request.Cookies("username")
response.write("Username: " & username)
%>

This code retrieves the value of the cookie named "username" and prints it in the browser. Note that cookies can be retrieved anywhere in the code, unlike creating them which has to be done before anything is written to the body of the response.

Cookies with Multiple Values

Cookies can also contain multiple values, using something called keys. Take a look at this example.

<%
Response.Cookies("user")("realname") = "John Doe"
Response.Cookies("user")("username") = "johnny55"
Response.Cookies("user")("age") = "55"
%>

This is fairly straightforward to understand. Retrieving the cookies is very simple too.

<%
realname = Response.Cookies("user")("realname")
username = Response.Cookies("user")("username")
age = Response.Cookies("user")("age")
%>

Listing All Cookies

You may want to list all the cookies available in a particular request. Here is how to do it.

<%
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
%>

The HasKeys property is used to check if a cookie has any keys.

The Cookies Collection

Here is a quick reference table of properties that you can set on cookies in ASP.

Property Description Access
Domain Tells the browser only to send the cookie to the specified domain. Write only
Expires Tells the browser to delete the cookie at the specified date. If no date is specified, the cookie expires when the browser is closed. Write only
HasKeys Determines whether the cookie has any keys. Read only
Path Tells the browser only to send the cookie to the specified path. Write only
Secure If set to True then the cookie will only be sent to the server when using a secure connection. This defaults to False. Write only
 
 

Home | eCommerce Shopping Cart | Affiliate Software | Website Analytics | eCommerce Merchant Accounts | Merchant Account Integration

Help & Support | Contact | Request a Quote | Affiliates | Developer Articles | Marketing Articles | Blog | Policies | Site Map

© 2008

Earth Skater  Boca Raton Web Design Florida FL USA  

Content last updated 7/30/2008