用户的浏览商品记录功能代码

用户的浏览商品记录功能代码
  1. /**
  2.  * 车辆历史浏览记录
  3.  * $data 车辆记录信息
  4.  */
  5. protected function _history($data)
  6. {
  7.   if(!$data || !is_array($data))
  8.   {
  9.     return false;
  10.   }
  11.   //判断cookie类里面是否有浏览记录
  12.   if($_COOKIE['history_car'])
  13.   {
  14.     $history = unserialize($_COOKIE['history_car']);
  15.     array_unshift($history, $data); //在浏览记录顶部加入
  16.     /* 去除重复记录 */
  17.     $rows = array();
  18.     foreach ($history as $v)
  19.     {
  20.       if(in_array($v, $rows))
  21.       {
  22.         continue;
  23.       }
  24.       $rows[] = $v;
  25.     }
  26.     /* 如果记录数量多余5则去除 */
  27.     while (count($rows) > 5)
  28.     {
  29.       array_pop($rows); //弹出
  30.     }
  31.     setcookie('history_car',serialize($rows),time()+3600*24*30,'/');
  32.   }
  33.   else
  34.   {
  35.     $history = serialize(array($data));
  36.     setcookie('history_car',$history,time()+3600*24*30,'/');
  37.   }
  38. }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现商品浏览记录功能,可以使用JSP中的session对象来存储浏览过的商品信息。 首先,在商品详情页中,可以通过JSP代码获取到当前浏览商品信息,并将其存储到session中。示例代码如下: ``` <% String productId = request.getParameter("productId"); // 获取当前浏览商品ID String productName = "商品名称"; // 根据商品ID查询商品名称等信息 String productPrice = "商品价格"; String productImage = "商品图片路径"; // 将商品信息存储到session中 HttpSession session = request.getSession(); List<String[]> historyList = (List<String[]>) session.getAttribute("historyList"); if (historyList == null) { historyList = new ArrayList<String[]>(); } String[] productInfo = {productId, productName, productPrice, productImage}; historyList.add(0, productInfo); if (historyList.size() > 5) { historyList.remove(5); } session.setAttribute("historyList", historyList); %> ``` 上述代码中,首先获取到当前浏览商品ID,然后查询该商品的名称、价格、图片路径等信息。接着,从session中获取商品浏览记录列表,如果列表不存在则创建一个新的列表。将当前浏览商品信息添加到列表的最前面,如果列表超过了5个则删除最后一个。最后,将更新后的列表重新存储到session中。 在商品浏览记录页面中,可以通过JSP代码获取到session中存储的商品浏览记录列表,并将其展示出来。示例代码如下: ``` <% HttpSession session = request.getSession(); List<String[]> historyList = (List<String[]>) session.getAttribute("historyList"); %> <!DOCTYPE html> <html> <head> <title>商品浏览记录</title> </head> <body> <h1>商品浏览记录</h1> <ul> <% for (String[] productInfo : historyList) { %> <li> <a href="productDetail.jsp?productId=<%=productInfo[0]%>"> <img src="<%=productInfo[3]%>" alt="<%=productInfo[1]%>"> <span><%=productInfo[1]%></span> </a> </li> <% } %> </ul> </body> </html> ``` 上述代码中,首先获取到session中存储的商品浏览记录列表。然后,使用JSP循环语句遍历列表中的每个商品信息,并将其展示出来。 以上就是实现商品浏览记录功能的JSP代码示例。需要注意的是,为了保证用户隐私安全,可能需要对商品浏览记录进行加密处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值