ASP – 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 for 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,2002#

%>

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)

%>

Output:

Firstname=Alex

A Cookie with Keys

If a cookie contains a collection of multiple values, we say that the cookie has Keys.

In the example below, we will create a cookie collection named “user”. The “user” cookie has Keys that contains information about a user:

<%

Response.Cookies(“user”)(“firstname”)=”John”

Response.Cookies(“user”)(“lastname”)=”Smith”

Response.Cookies(“user”)(“country”)=”Norway”

Response.Cookies(“user”)(“age”)=”25″

%>

Read all Cookies

Look at the following code:

<%

Response.Cookies(“firstname”)=”Alex”

Response.Cookies(“user”)(“firstname”)=”John”

Response.Cookies(“user”)(“lastname”)=”Smith”

Response.Cookies(“user”)(“country”)=”Norway”

Response.Cookies(“user”)(“age”)=”25″

%>

Assume that your server has sent all the cookies above to a user.

Now we want to read all the cookies sent to a user. The example below shows how to do it (note that the code below checks if a cookie has Keys with the HasKeys property):

<html>

<body>

<%

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

%>

</body>

</html>

Output:

firstname=Alex

user:firstname=John
user:lastname=Smith
user: country=Norway
user: age=25

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )
Advertisements

2 thoughts on “ASP – What is a Cookie?

    • @Asif
      Nice Question

      1) Cookies are a small token of data stored in browser by any program or a web site .. EX. in Gooogle email login it will have an option remember me what exactly it does is that stores your Id and password in browser in encrypted format and all sites have their own and secure algorithm.
      2) can we edit? Practically No………..!!!
      Beacuse say suppose we could edit and by that some hacker gets it what it could do … get ur credit card info. ur user Id password. So think what can happen to ur personal Data ..

      When i was studying i also got the same doubt.. searched for it but could never find an answer and i was so facinated by it that i developed a project on this but later found that if you somehow know that algorith also no use…. that have specific key to encrypt …
      after that also it is not possible that u will the desired result.

      so practically u can never edit a cookie.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s