Home | Advertising Info14 USERS CURRENTLY ONLINE   
PowerASP
   Site Search Contact Us Saturday, May 04, 2024  

  Active InfoActive Info  Display List of Info MembersMemberlist  Search The InfoSearch  HelpHelp
  RegisterRegister  LoginLogin
Classic ASP - Advanced
 PowerASP Code Help Area : Classic ASP - Advanced
Subject Info: How to create cookies using ASP A d d  -  P o s tAdd P o s t
Author
Message << Prev Info | Next Info >>
cwilliams
Admin Group
Admin Group
Avatar

Joined: April/26/2004
Location: United States
Online Status: Offline
Info: 137
Added: April/27/2004 at 8:12pm | IP Logged Quote cwilliams

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"))
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 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.



__________________


Chris Williams
http://www.PowerASP.com
Back to Top View cwilliams's Profile Search for other info by cwilliams
 
ferretneck
Newbie
Newbie


Joined: June/21/2004
Online Status: Offline
Info: 2
Added: June/21/2004 at 7:15am | IP Logged Quote ferretneck

Hi,

On the subject of cookies in asp, is there a way of adding 'records' to the cookie to enable (for instance) a list of visited sites for a user to be added and recalled when the user logs on to our site?

I have managed to add single records of websites visited within our site, but I can't get it to add more - only overwrite.

This is what I am using:

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_star_STRING
Recordset1.Source = "SELECT * FROM TABLE WHERE TABLEID = " + Replace(Recordset1__MMColParam, "'", "''") + ""
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

Recordset1_numRows = 0
Msite=(Recordset1.Fields.Item("TABLEWebsite").Value)
%>
<%
Response.Cookies("firstname") ("Key1") =Msite
Response.Cookies("firstname").Expires=#May 10,2006#
%>

Obviously it just overwrites anything in the Msite string. I need a way of adding sites instead of overwriting.

Cheers

Back to Top View ferretneck's Profile Search for other info by ferretneck
 
Hypa
Newbie
Newbie
Avatar

Joined: June/29/2004
Location: United States
Online Status: Offline
Info: 6
Added: July/01/2004 at 6:25pm | IP Logged Quote Hypa

add a check and increment the value of the Key... Key1, Key2 etc...  you can read your cookie and check for the current last value of KeyX then add 1 write msite to that.  I think that is what you are trying to do.. Sorry if Im way off base.

__________________
Never Mess with a geek and his toys!
Back to Top View Hypa's Profile Search for other info by Hypa
 
ferretneck
Newbie
Newbie


Joined: June/21/2004
Online Status: Offline
Info: 2
Added: July/02/2004 at 4:40am | IP Logged Quote ferretneck

Yep thats exactly what I'm trying to do!

Although, as I am new to writing cookies I've got less than no idea how to code it :)

Any chance of some pointers ?

Cheers

Back to Top View ferretneck's Profile Search for other info by ferretneck
 

If you wish to make a comment to this info you must first login
If you are not already registered you must first register

  A d d  -  P o s tAdd P o s t
Printable version Printable version

Info Jump
You cannot add new info in this area
You cannot add to info in this area
You cannot delete your info in this area
You cannot edit your info in this area
You cannot create polls in this area
You cannot vote in polls in this area

   Active Server Pages Rule The World
Contact Us  
All artwork, design & content contained in this site are Copyright © 1998 - 2024 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 asp search engine active server page asp application components tutorial CJWSoft ASPProtect ASPBanner ASPClassifieds www.aspprotect.com, www.cjwsoft.com,www.aspclassifieds.com,www.aspphotogallery.com