Cookies OR Session variables

Results 1 to 2 of 2

Thread: Cookies OR Session variables

  1. #1
    Join Date
    Dec 1969

    Default Cookies OR Session variables

    I have a shopping cart app that uses session variables to keep track of items in the cart. I would like to convert this to cookies instead in order to keep the cart contents for longer periods of time.<BR><BR>Can anyone suggest how to do this? Can I use both?<BR><BR>I would also like to store the user&#039s profile in a cookie to avoid them having to rekey their information when ordering products. Any ideas on how to accomplish this? Does this require inserts into the database or can all this info be stored in cookies?

  2. #2
    Hayden Rogers Guest

    Default RE: Cookies OR Session variables

    This is really quite a simple process. My favourite technique is to create a cookie with multiple keys which are named for the product code and then populated with the quantity ordered. An example:<BR><BR>&#060;%<BR>strList = "ShoppingCart" & CStr(Date) &#039 Insert your own name!<BR>Response.Cookies(strList) = "ShoppingCart List etc blah"<BR>Response.Cookies.Expires = (Now() + 365) &#039 This is a year!<BR>For Each Item in (however you do this is up to you)<BR>Response.Cookies(strList)(Item)=(QuantityV ariable)<BR>next<BR>%&#062;<BR><BR>You might want to put this either at the beginning of the page or in its own page to avoid conflict with any HTTP headers.<BR><BR>To retrieve the lists:<BR><BR>&#060;%<BR>For Each Item in Response.Cookies<BR>If InStr(Item,"ShoppingCart") Then<BR>For Each OrderItem in Response.Cookies(Item)<BR><BR>&#039 Do what you want here - OrderItem will be the product code and Response.Cookies(Item)(OrderItem) will be the quantity!<BR><BR>next<BR>end if<BR>next<BR>%&#062;<BR><BR>Storing the name etc is simple:<BR><BR>&#060;%<BR>Response.Cookies("userde tails")("username") = Session("username")<BR>Response.Cookies("userdetai ls")("email") = Session("email")<BR>Response.Cookies("userdetails" )("gender") = Session("gender")<BR>&#039 and so on...<BR>Response.Cookies("userdetails").Expires = (Now()+365) &#039 a year...<BR>%&#062;<BR><BR>Pretty simple stuff really - I know the code is far from complete but I hope that gives you an idea. Remember to always give an expiry date or the cookies will wxpire as soon as the browser is closed.<BR><BR>An to remove a cookie:<BR><BR>&#060;%<BR>Response.Cookies("userde tails").Expires = (Now()-365) &#039 Expired a year ago!<BR>%&#062;<BR><BR>Alternatively, store all information in a database and give them a user id and account - then simply store the user id in a cookie... (dangerous stuff for people who share computers though).<BR><BR>Good Luck<BR>HR<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts