FREE tutorial,solution,RSS Feeds on Operating Systems, Programming, Web Development, Applications, Databases, Networking, Hardware, Security, SEO Free Expertsforge Membership
Join us as Moderator
Submit Article to Expertsforge.com Submit Article My Expertsforge
 
RSS Feeds, Help Help RSS Feeds
bannertop
 

ASP Tutorial: How to create and retrieve cookies using ASP

jawahar
4/2/2005 3:57:33 PM, Views: 1686
Cookies are easy to create and retrieve using ASP.

They can be used to remember things about a user when they come back to
your site. Cookies expire after a certain amount of time which you can
set. Also, the clients browser must have cookies enabled for them to
work.

Here is a very simple example of creating a cookie called "MYCOOKIE" with
a key called "BgColor" and setting the cookie to expire in one year.  In
this example there is only one key, but cookies can have multiple keys.

<%
Response.Cookies ("MYCOOKIE")("BgColor") = "Blue"
Response.Cookies ("MYCOOKIE").Expires = DATE + 365
%>

If you dont set the "expires" time the cookie will expire right away so
dont leave that part out.

Please Note: You must create the cookie before the HTTP headers are
written to the client browser. Basically that means you need to create
cookies before the <HTML> tag on your page.

However if you turn on buffering like so <% Response.Buffer = True %>
then you can create the cookie wherever you want within your code because
when buffering is turned on no information is written to the client
browser until the all the code is
finished running. The <% Response.Buffer = True %> needs to go right
under the
<%@LANGUAGE="VBSCRIPT" %> on your page.

Also dont use underscores or other strange characters when naming your
cookies because they may not work.

Here is are examples of retrieving the cookie.

Do this if you dont care if the cookie exists or not.
<%
Response.Write(Request.Cookies ("MYCOOKIE")("BgColor"))
%>

Do something like this if you want to check first to see if the cookie
exists which is usually a good idea.
<%
If Request.Cookies ("MYCOOKIE")("BgColor") <> "" Then
Response.Write(Request.Cookies ("MYCOOKIE")("BgColor"))
Else
you could do something like ask them to pick a BgColor and then set the
cookie again
or you could just give them a default value for the Bgcolor since they
dont have a cookie
End If
%>


When you create a cookie the info is saved on the clients computer.
With IE a text file is created in the cookies directory of the clients
computer. It keeps track of the cookie. If they are using netscape there
is a file called cookies.txt where the cookie info piles up. Either way
if they delete the cookie info the cookies will be gone when they return
to your site.

There is a lot more to using cookies but this is the basic info you need
to know to create and retrieve them.
Next Steps:
Add this Tutorial to:
Blink Blink del.icio.ous Del.icio.us Digg Digg
Fark Fark Furl Furl Google Google
Reddit Reddit Simpy Simpy Spurl Spurl
Technorati Technorati Windows Live Win Live Yahoo Yahoo
Rate Me!
Not Yet Rated!
Rate:
Send Private MessageSend Message
Signup / Login To View the Solution or Provide Comments
Post Comment/Solution
Comment:*
        (Link Rules) 
  Use : [bold] for <b>; [/bold] for </b>; [italic] for <i>; [/italic] for </i>; [code] & [/code] for code
 
Categories
Options
ASP RSS Feed
Most Popular Tutorial
Most Popular Solution
Top Rated
Top Contributors
Yearly
Overall
 
1. jawahar (200)
bnrtop