Home | Advertising Info68 USERS CURRENTLY ONLINE   
   Site Search Contact Us Sunday, August 07, 2022  

How to create and retrieve cookies using ASP

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 don't set the "expires" time the cookie will expire right away so don't 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 don't 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 don't 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"))
'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 don't 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.


   Active Server Pages Rule The World
Contact Us  
All artwork, design & content contained in this site are Copyright 1998 - 2022 PowerASP.com and Christopher J. Williams
Banner ads ,other site logos, etc are copyright of their respective companies.
STATS Unless otherwise noted - All Rights Reserved.

Active Server Pages what is asp programming how to sample asp example code scripts software asp forum mail sessions applications global.asa CJWSoft ASPProtect ASPBanner ASPClassifieds www.aspclassifieds.com, www.powerasp.com,www.cjwsoft.com,www.aspphotogallery.com,www.codewanker.com,www.aspprotect.com,www.aspbanner.com