Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Recently Visited Products-Setting a cookie
New Posts  All Forums:Forum Nav:

Recently Visited Products-Setting a cookie

post #1 of 3
Thread Starter 
An example of this "Recently Visited Products" feature is on www.officedepot.ca

To show recently visited products on a Website, I heard you must set a cookie which contains the recently visited products referenced by product number. Then call the cookie and display the value.

Does anyone know how to do this..
post #2 of 3
Doing something like this shouldn't prove to be too hard, but it does depend on what you are using to build the program. You will need a dynamic backend like Ruby, ASP, php, etc. Being an asp.net developer, I can show you as simply as I can how to do it in asp/c#.

Basically all you need to do is initialize a cookie, and set the values to update every time the user looks at a new item.

I included all code for a very basic (and ugly) example in asp so you can get a look at it.

Code:

<&#37;@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <h1><asp:Label id="title" runat="server" Text="Select Item" /></h1>
   <a href="?Item=Opteron144">Opteron144</a><br />
   <a href="?Item=Opteron1210">Opteron1210</a><br />
   <a href="?Item=E6600">E6600</a><br />
   <a href="?Item=Q6600">Q6600</a><br /><br />
   <div style="border:solid 2px black;padding:3px;">
    Recently Viewed Items:
    <div style="border:dotted 1px blue;margin:2px;"><asp:Label ID="Label1" runat="server" /></div>
    <div style="border:dotted 1px blue;margin:2px;"><asp:Label ID="Label2" runat="server" /></div>
   </div> 
  </div>
 </form>
</body>
</html> 
and here is the codebehind page:
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
publicpartialclasstest2 : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
//these 3 lines makes sure that no empty cookies will be called:
Response.Cookies["Items"]["I1"] = Request.Cookies["Items"]["I1"];
Response.Cookies["Items"]["I2"] = Request.Cookies["Items"]["I2"];
Response.Cookies["Items"].Expires = DateTime.Now.AddDays(30);
//if there is something in Items/I1, it will display
if (!Request.Cookies["Items"]["I1"].ToString().Equals(""))
Label1.Text = Request.Cookies["Items"]["I1"].ToString();
else//or it will show whatever you put here
Label1.Text = "No Recent Item (1)";
//repeat for recent item 2
if (!Request.Cookies["Items"]["I2"].ToString().Equals(""))
Label2.Text = Request.Cookies["Items"]["I2"].ToString();
else
Label2.Text = "No Recent Item (2)";
//if there is a request for a new item in the url
if (Request.QueryString["Item"] != null)
{
//if user looks at a new item, call the addItem function
addItem(Request.QueryString["Item"].ToString());
//and show the new item
title.Text = "Now Viewing " + Request.QueryString["Item"].ToString();
}
}
protectedvoid addItem(string newItem)
{
// moves item 1 into the item 2 'slot'
Response.Cookies["Items"]["I2"] = Request.Cookies["Items"]["I1"];
// then replaces item 1 with the new item
Response.Cookies["Items"]["I1"] = newItem;
}
}
Everything in green is comments and explains to you what is going on.

First thing is that the cookie is established and the expiration is set to 30 days in the future. Then each time a new item is looked at, it is sent as a querystring (in the url). There is many other ways to do this, but this is the easiest to illustrate. Once the program sees a new item is being looked at, it updates the cookie to show the new item as recently viewed and pushes the old #1 item to the #2 spot.

Hope this sheds some light on your problem. Good luck!
post #3 of 3
Thread Starter 
hi,

i dont run php, i run html, so the code you provided don't think will work.
i found this code, would this work? Can you look over it; thanks..

Code:
<script>
function setCookie(name, value) {
var today = new Date()
var expires = new Date()
expires.setTime(today.getTime() + 1000*60*60*24*30) // expires in a month
document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +";path=/"
}

function getCookie(Name) {
var search = Name + "="
if(document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if(offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset)
if(end == -1) end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
else return ""
}
}

//get information of previous item
var RecentlyViewNAME=getCookie("recNAME");
var RecentlyViewNO=getCookie("recNO");
var RecentlyViewIMAGE=getCookie("recIMAGE");

//set information for current item
proIMAGE="<ss:value source="$product.thumbnailImageName"/>";
proNAME="<ss:value source="$product.name"/>";
proNO="<ss:value source="$product.productNumber"/>";

if(RecentlyViewNO){ //if not the first viewed item

document.write('<table width="100%" align="center" cellpadding="2" cellspacing="2"><tr><td align="center">');
document.write('<b>Previous Viewed Item:</b><br>');

document.write('<img src="'+RecentlyViewIMAGE+'" border="0">');//the image path here maybe different in your site
document.write('<br>'+RecentlyViewNAME);


document.write('</td></tr></table>');

//update the previous vieded item
setCookie("recIMAGE",proIMAGE);
setCookie("recNAME",proNAME);
setCookie("recNO",proNO);


}else{ //if first vieded item

//set as previous vieded item
setCookie("recIMAGE",proIMAGE);
setCookie("recNAME",proNAME);
setCookie("recNO",proNO);

}

</script>
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Recently Visited Products-Setting a cookie