cookies实现最近浏览商品 如何实现浏览历史

&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
使用cookies查询商品浏览记录
摘要:经历了俩个星期,易买网项目如期完工,现在总结一下如何使用cookies实现浏览商品的历史记录。第一步:创建商品实体类。第二步:连接oracle数据库。第三步:创建商品三层架构。效果图:&在要显示的地方加入核心代码:&h2&最近浏览&/h2&&divstyle=&height:200display:overflow:hidden&&&%//获取所有的cookieCookie[]cookie
经历了俩个星期,易买网项目如期完工,现在总结一下如何使用cookies实现浏览商品的历史记录。
第一步:创建商品实体类。
第二步:连接oracle数据库。
第三步:创建商品三层架构。
在要显示 的地方加入核心代码:&h2&最近浏览&/h2& &div style=&height:200display:overflow:hidden&& &%
//获取所有的cookie Cookie[] cookies = request.getCookies(); //筛选cookie if(cookies != null &;&; cookies.length&0){ for(int j = cookies.length-1; j&=0; j--){ String cookieName = cookies[j].getName(); if(cookieName.startsWith(&product-view&)){ //调用查询商品的方法 List&easybuy_product& list4 = productbiz.product(cookies[j].getValue()); request.setAttribute(&list4&,list4); %& &c:forEach var=&hao& items=&${requestScope.list4}& & &dl class=&clearfix&& &dt&&img src=&${hao.ep_file_name}& width=&50px&/&&/dt& &dd&&a href=&addcookie?id=${hao.ep_id}& target=&_blank&&${hao.ep_name}&/a&&/dd& &/dl& &/c:forEach& &%}}}%& &/div&点击跳转查看商品详细页代码如下:&%String path = request.getContextPath();String basePath = request.getScheme()+&://&+request.getServerName()+&:&+request.getServerPort()+path+&/&;//获取商品idrequest.setCharacterEncoding(&utf-8&);String book = request.getParameter(&id&);Cookie[] cookies = request.getCookies();List&Cookie& cookieList = new ArrayList&Cookie&();Cookie tempCookie =null;//筛选cookieif(cookies != null &;&; cookies.length&0){ for(Cookie c:cookies){ String cookieName = c.getName(); if(cookieName.startsWith(&product-view&)){ cookieList.add(c); } if(c.getValue().equals(book)){ tempCookie = } }} if(cookieList.size()&=10 &;&; tempCookie==null){ tempCookie = cookieList.get(0);}Cookie cookie = new Cookie(&product-view&+book,book);response.addCookie(cookie);%&这样就实现了商品的浏览记录。&
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
新用户大礼包!
现在注册,免费体验40+云产品,及域名优惠!
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
使用cookies查询商品浏览记录相关信息,包括
的信息,所有使用cookies查询商品浏览记录相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
International编程实现cookies浏览历史记录功能_西西软件资讯
西西软件园多重安全检测下载网站、值得信赖的软件下载站!
→ 编程实现cookies浏览历史记录功能
V4.1.8.8 绿色汉化版
类型:卸载删除大小:1.3M语言:中文 评分:8.3
&1.创建历史记录的实体类public class LastProducts{private int _private int _private string _private string _public LastProducts(int id,int typeid,string imgsrc,string restorename){_productid =_categoryid =_imgsrc =_productname =}public int ProductId{get { return _ }}public int CategoryId{get { return _ }}public string ImgSrc{get { return _ }}public string ProductName{get { return _ }}}2.定义存储cookies的方法public void HistoryRestore(string cookieName,int objectID){HttpRequest Request = HttpContext.Current.RHttpResponse Response = HttpContext.Current.Rif (Request.Cookies[cookieName] != null){HttpCookie tempCurBuyerList = Request.Cookies[cookieName];string tempstr = tempCurBuyerList.Vif (tempstr.IndexOf(",") & 0){string[] sArray = tempstr.Split(',');bool hasthis =foreach (string s in sArray){if (s == objectID.ToString()){hasthis =}else{hasthis =}}if (!hasthis) //如果没有ID,则加入{if (sArray.Length & 3) //3为存储浏览记录数的数量,实际数量为7{// 超过数量,去掉最先入队的元素tempstr = tempstr.Substring(0, tempstr.LastIndexOf(","));}// 队列tempstr = objectID.ToString() + "," +}}else{//tempstr += "," + objectID.ToString(); if (tempstr != objectID.ToString()){tempstr = objectID.ToString() + "," +}}tempCurBuyerList.Value =tempCurBuyerList.Expires = DateTime.Now.AddDays(1);Response.Cookies.Add(tempCurBuyerList);//或者 Response.Cookies[cookieName].Value =}else{HttpCookie addToCookies = new HttpCookie(cookieName);addToCookies.Value = objectID.ToString();addToCookies.Expires = DateTime.Now.AddDays(1);Response.Cookies.Add(addToCookies);}}3.读取cookies存储数据public List&LastProducts& GetLastProducts(){HttpRequest Request = HttpContext.Current.RList&LastProducts& list =if (Request.Cookies["restoreid"] != null){HttpCookie tempCurBuyerList = Request.Cookies["restoreid"];string[] strArr = tempCurBuyerList.Value.Split(',');list = new List&LastProducts&();foreach (string s in strArr){ShopProduct model = dal.GetProById(int.Parse(s)); //商品的实体类if (model != null){list.Add(new Model.Shop.LastProducts(model.ProductID, model.CategoryID, model.ImageHref, model.Name));}}}}4.在用户浏览某产品时记录到cookies中:HistoryRestore("restoreid",productId);5.数据源的绑定  Repeater1.DataSource = GetLastProducts();Repeater1.DataBind();
03-1811-0911-1001-1204-2601-2201-2201-1701-1701-14
阅读本文后您有什么感想? 已有23人给出评价!
名称大小下载js 记录用户浏览历史记录 - ThinkPHP框架
一个实现记录浏览历史的javascript工具
此版本有bug 建议用一下这个
&*&History
&*&&var&his&=&new&History('key');&&//&参数标示cookie的键值
&*&his.add(&标题&,&&连接&比如&http://www.baidu.com&,&&其他内容&);&
&*&得到历史数据&返回的是json数据
&*&var&data&=&his.getList();&&//&[{title:&标题&,&&link&:&&http://www.baidu.com&}]
function&History(key)&{
&&&&this.limit&=&10;&&//&最多10条记录
&&&&this.key&=&key&||&'y_his';&&//&键值
&&&&this.jsonData&=&&&//&数据缓存
&&&&this.cacheTime&=&24;&&//&24&小时
History.prototype&=&{
&&&&constructor&:&History
&&&&,addCookie:&function(name,&value,&expiresHours,&options)&{
&&&&&&&&options&=&options&||&{};
&&&&&&&&var&cookieString&=&name&+&&=&&+&escape(value);&
&&&&&&&&//判断是否设置过期时间&
&&&&&&&&if(undefined&!=&expiresHours&&&&expiresHours&&&0){&
&&&&&&&&&&&&var&date&=&new&Date();&
&&&&&&&&&&&&date.setTime(date.getTime()&+&expiresHours&*&3600&*&1000);&
&&&&&&&&&&&&cookieString&=&cookieString&+&&;&expires=&&+&date.toUTCString();&
&&&&&&&&}&
&&&&&&&&var&path&=&options.path&?&';&path='&+&options.path&:&'',
&&&&&&&&&&&&domain&=&options.domain&?&';&domain='&+&options.domain&:&'',
&&&&&&&&&&&&secure&=&options.secure&?&';&secure'&:&'';
&&&&&&&&//alert(cookieString&+&path&+&domain&+&secure);
&&&&&&&&document.cookie&=&cookieString&+&path&+&domain&+&&
&&&&,getCookie:&function(name)&{&
&&&&&&&&var&cookies&=&document.cookie,&&//得到本域下的所有cookie&&--&&userId=828;&userName=lisi&
&&&&&&&&&&&&arrCookie&=&cookies.split(&;&),
&&&&&&&&&&&&val&=&&&,
&&&&&&&&&&&&tmpArr&=&&&;
&&&&&&&&for(var&i=0;&i&arrCookie.&i++)&{&
&&&&&&&&&&&&tmpArr&=&arrCookie[i].split(&=&);&
&&&&&&&&&&&&if(tmpArr[0]&==&name)&{
&&&&&&&&&&&&&&&&val&=&unescape(tmpArr[1]);
&&&&&&&&&&&&&&&&
&&&&&&&&&&&&}
&&&&&&&&return&val.toString();&
&&&&,deleteCookie:&function(name)&{
&&&&&&&&var&date&=&new&Date();&
&&&&&&&&date.setTime(date.getTime()-10000);&
&&&&&&&&document.cookie&=&name&+&&='';&expires=&+date.toUTCString();&
&&&&,initRow&:&function(title,&link,&other)&{
&&&&&&&&return&'{&title&:&'+title+'&,&&link&:&'+link+'&,&&other&:&'+other+'&}';
&&&&,parse2Json&:&function(jsonStr)&{
&&&&&&&&var&json&=&[];
&&&&&&&&try&{
&&&&&&&&&&&&json&=&JSON.parse(jsonStr);
&&&&&&&&}&catch(e)&{
&&&&&&&&&&&&//alert('parse&error');
&&&&&&&&&&&&json&=&eval(jsonStr);
&&&&&&&&return&
&&&&//&添加记录
&&&&,add&:&function(title,&link,&other)&{
&&&&&&&&var&jsonStr&=&this.getCookie(this.key);
&&&&&&&&//alert(jsonStr);&
&&&&&&&&if(&&&!=&jsonStr)&{
&&&&&&&&&&&&this.jsonData&=&this.parse2Json(jsonStr);
&&&&&&&&&&&&
&&&&&&&&&&&&//&排重
&&&&&&&&&&&&for(var&x=0;&x&this.jsonData.&x++)&{
&&&&&&&&&&&&&&&&if(link&==&this.jsonData[x]['link'])&{
&&&&&&&&&&&&&&&&&&&&return&
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}
&&&&&&&&&&&&//&重新赋值&组装&json&字符串
&&&&&&&&&&&&jsonStr&=&'['&+&this.initRow(title,&link,&other)&+&',';
&&&&&&&&&&&&for(var&i=0;&i&this.limit-1;&i++)&{
&&&&&&&&&&&&&&&&if(undefined&!=&this.jsonData[i])&{
&&&&&&&&&&&&&&&&&&&&jsonStr&+=&this.initRow(this.jsonData[i]['title'],&this.jsonData[i]['link'],&this.jsonData[i]['other'])&+&',';
&&&&&&&&&&&&&&&&}&else&{
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}
&&&&&&&&&&&&jsonStr&=&jsonStr.substring(0,&jsonStr.lastIndexOf(','));
&&&&&&&&&&&&jsonStr&+=&']';
&&&&&&&&&&&&
&&&&&&&&}&else&{
&&&&&&&&&&&&jsonStr&=&'['+&this.initRow(title,&link,&other)&+']';
&&&&&&&&this.jsonData&=&this.parse2Json(jsonStr);
&&&&&&&&this.addCookie(this.key,&jsonStr,&this.cacheTime);
&&&&//&得到记录
&&&&,getList&:&function()&{
&&&&&&&&//&有缓存直接返回
&&&&&&&&if(null&!=&this.jsonData)&{
&&&&&&&&&&&&return&this.jsonD&&//&Array
&&&&&&&&}&
&&&&&&&&//&没有缓存从&cookie&取
&&&&&&&&var&jsonStr&=&this.getCookie(this.key);
&&&&&&&&if(&&&!=&jsonStr)&{
&&&&&&&&&&&&this.jsonData&=&this.parse2Json(jsonStr);
&&&&&&&&return&this.jsonD
&&&&//&清空历史
&&&&,clearHistory&:&function()&{
&&&&&&&&this.deleteCookie(this.key);
&&&&&&&&this.jsonData&=&
积分:2674
ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。asp.net 使用cookies或者session实现浏览历史记录功能
cooies实现方式:
读取cookies存储数据
/// &summary&
///HistoryRestore 的摘要说明
///最近浏览记录
/// &/summary&
public class HistoryRestore
public HistoryRestore()
//TODO: 在此处添加构造函数逻辑
public static void HistoryRestoreList(string cookieName, int objectID)
HttpRequest Request = HttpContext.Current.R
HttpResponse Response = HttpContext.Current.R
if (Request.Cookies[cookieName] != null)
HttpCookie tempCurBuyerList = Request.Cookies[cookieName];
string tempstr = tempCurBuyerList.V
if (tempstr.IndexOf(",") & 0)
string[] sArray = tempstr.Split(',');
bool hasthis =
foreach (string s in sArray)
if (s == objectID.ToString())
if (!hasthis)
//如果没有ID,则加入
if (sArray.Length & 11) //3为存储浏览记录数的数量,实际数量为7
// 超过数量,去掉最先入队的元素
tempstr = tempstr.Substring(0, tempstr.LastIndexOf(","));
tempstr = objectID.ToString() + "," +
//tempstr += "," + objectID.ToString();
if (tempstr != objectID.ToString())
tempstr = objectID.ToString() + "," +
tempCurBuyerList.Value =
tempCurBuyerList.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(tempCurBuyerList);
//或者 Response.Cookies[cookieName].Value =
HttpCookie addToCookies = new HttpCookie(cookieName);
addToCookies.Value = objectID.ToString();
addToCookies.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(addToCookies);
}在用户浏览某产品时记录到cookies中:
HistoryRestore.HistoryRestoreList("restoreid", int.Parse(pid));
读取cookies存储数据
List&VCtuan.Model.Tb_Product& proList =
if (Request.Cookies["restoreid"] != null)
HttpCookie tempCurBuyerList = Request.Cookies["restoreid"];
string[] strArr = tempCurBuyerList.Value.Split(',');
proList = new List&VCtuan.Model.Tb_Product&();
foreach (string s in strArr)
VCtuan.Model.Tb_Product model = proBLL.GetModel(int.Parse(s)); //商品的实体类
if (model != null)
proList.Add(model);
RptLastProductList.DataSource = proL
RptLastProductList.DataBind();
http://www.cnblogs.com/woshilee/articles/2391722.html
使用session方式:
List&string& list =
if (Session["track"] == null)
list = new List&string&();
list = (List&string&)Session["track"];
if (list.Contains(pid))
list.Remove(pid);
// 如果这次浏览的商品在浏览记录中,删除后重新添加进去,保持浏览顺序
if (list.Count == 10)
list.RemoveAt(0);
// 浏览记录保存10个,到了10个删除最老的一个记录.
list.Add(pid);
// 浏览顺序为list[9] list[8].....list[1] list[0]
Session["track"] =
// 把list更新到Session
///////////////////////////////////////////////////////////////
if (list.Count & 0)
List&VCtuan.Model.Tb_Product& proList = new List&VCtuan.Model.Tb_Product&();
for (int i = list.Count - 1; i &= 0; i--)
string proId = list[i];
proList.Add(proBLL.GetModel(int.Parse(proId)));
RptLastProductList.DataSource = proL
RptLastProductList.DataBind();
http://zhidao.baidu.com/question/.html
没有更多推荐了,

我要回帖

更多关于 ben's cookies 的文章

 

随机推荐