Learn about ASP Cookies (10 ASP):

ASP Cookies

A cookie is basically a small file which is embedded on the user computer by the server to identify the user. This cookie is send with every request that user make for a particular page using any web browser. One of the many advantages of ASP is to both retrieve and create different cookie values. Following is the brief method of crating cookies in ASP:

The command that is used to produce cookies is:    “Response.Cookies”. This command should be written before the HTML tags in the file. (<html>).  The syntax for creating cookies is following:

<%

Response.Cookies”name of cookie”=”value of cookie”.

%>

There are different properties that any cookie can have for example the expiry date of the cookie.  Following is the example where a cookie named “website-school” is created with the value “ASP Tutorial” and will expire on 03/01/2012:

<%

Response.Cookies” website-school”=” ASP Tutorial”.

Response.Cookies” website-school”=” ASP Tutorial”.Expires=#January 03,2012#

%>

Now comes the part where you get to learn how to get the cookie. The command which is used to retrieve any created cookie is “Request.Cookies”. You can request the value of cookie and store it in a variable (Cookievariable) and then print it later. Following is the syntax used for this purpose:

<%

Cookievariable=Request.Cookies(”name of cookie”)

Response.write(”name of cookie” & Cookievariable)

%>

The following example will retrieve “website-school” and print its value:

<%

Cookievariable=Request.Cookies(”website-school”)

Response.write(”website-school” & Cookievariable)

%>

Output of this example will be:

website-school = ASP Tutorial

 

Now, some information about cookies with keys….

If any cookie comprises of more than one value, then such cookie is known as a cookie with keys. The syntax for doing so is:

<%

Response.Cookies(”name of cookie”) (“sub name”)=”value of cookie”.

Response.Cookies(”name of cookie”) (“sub name1”)=”value of cookie”.

%>

Following cookie will display cookies with keys of a user containing his name, age and sex:

<%

Response.Cookies(”user1”) (“name”)=”Muntaha”

Response.Cookies(”user1”) (“age”)=”22”

Response.Cookies(”user1”) (“sex”)=”Female”

%>

Reading all cookies send by a server will be very troublesome. Don’t worry there is a solution  to read all the cookies send by a server. But to do this we will use Request.Cookies.HasKeys property.  Following is the syntax to do this:

<script type=”text/javascript”><!–
google_ad_client = “ca-pub-7789281879799866”;
/* webpost */
google_ad_slot = “1930114734”;
google_ad_width = 300;
google_ad_height = 250;
//–>
</script>
<script type=”text/javascript
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>

<html>
<body>

<%
dim a,b
for each a in Request.Cookies
response.write(“<p>”)
if Request.Cookies(a).HasKeys then
for each b in Request.Cookies(a)
response.write(a &b& “=” & Request.Cookies(a)(b))
response.write(“<br />”)
next
else
Response.Write( “Start”)
end if
response.write “</p>”
next
%>
</body>
</html>

The output of this code will be as following:

Start

user1:name=Muntaha
user1:age=22
user1:sex=Female
Some of the browser does not support cookies. Then passing information via these browser is impossible. So the two alternative ways of passing information are:

  • You can either add Parameters to the URL.
  • Or you can use a form to do so

 

By using the forms, user can enter the information and then clicks the “Send” button. Following is the way with which you can add parameters to the URL:

<form method=”post” action=”none.asp”>
user Name: <input type=”text” name=”name” value=”” />
user age: <input type=”text” name=”age” value=”” />
<input type=”submit” value=”Submit” />
</form>

Similarly the retrieval of these cookies can be done in following way:

<%
name=Request.form(“name”)
age=Request.form(“age”)
response.write(“<p>” & name & ” ” & age & “!</p>”)
%>

Similarly the information can be passed by adding parameters to the URL.

<a href=”none.asp?name=Muntaha & age=22″>Go to none Page</a>

The retrieval of these cookies can be done in following way:


<%
name=Request.form(“name”)
age=Request.form(“age”)
response.write(“<p>” & name & ” ” & age & “!</p>”)
%>

For more visit next posts in this category.To go back to Contents of this tutorial: ASP Active Server Side Scripting Contents

email

One Response to Learn about ASP Cookies (10 ASP):

  1. Your post provided us with valuable information to help us begin|.

Leave a Reply