Asp.net Classic

Cookies

Asp.net Classic / Cookies

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("

") Next %>

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:

 

Go to Welcome Page

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:

 

First Name: Last Name:

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!

") %>

Technology
Asp.net Classic
want to connect with us ?
Contact Us