PHP记录用户访问记录,制作wordpress网站用户最近浏览记录
先写入cookie ,在PHP里Cookie的使用是有一些限制的。
1、使用setcookie必须在标签之前
2、使用setcookie之前,不可以使用echo输入内容
3、直到网页被加载完后,cookie才会出现
4、setcookie必须放到任何资料输出浏览器前,才送出
所以把一下内容放到头部顶端html之前
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?php $id=get_the_ID(); $TempNum = 12;//cookie里面存储多少个浏览记录 if(isset($_COOKIE['RecentlyGoods']))//判断是否设置了COOKIE { $RecentlyGoods=$_COOKIE['RecentlyGoods']; $RecentlyGoodsArray=explode(",", $RecentlyGoods); $RecentlyGoodsNum=count($RecentlyGoodsArray); //RecentlyGoodsNum 当前存储的变量个数 if (in_array($id, $RecentlyGoodsArray)) { //echo "已经存在,则不写入COOKIES <hr />"; } else { if($RecentlyGoodsNum < $TempNum) //如果COOKIES中的元素小于指定的大小,则直接进行输入COOKIES { if($RecentlyGoods=="") { setcookie("RecentlyGoods", $id, time()+3600*13, '/'); } else { $RecentlyGoodsNew=$RecentlyGoods.",".$id; setcookie("RecentlyGoods", $RecentlyGoodsNew,time()+3600*13, '/'); } } else //如果大于了指定的大小后,将第一个给删去,在尾部再加入最新的记录。 { $pos=strpos($RecentlyGoods,",")+1; //第一个参数的起始位置 $FirstString=substr($RecentlyGoods,0,$pos); //取出第一个参数 $RecentlyGoods=str_replace($FirstString,"",$RecentlyGoods); //将第一个参数删除 $RecentlyGoodsNew=$RecentlyGoods.",".$id; //在尾部加入最新的记录g setcookie("RecentlyGoods", $RecentlyGoodsNew,time()+3600*13,'/'); } } } else { setcookie("RecentlyGoods",$id,time()+3600*13, '/'); } |
读取Cookie 放到自己需要的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $str=$_COOKIE['RecentlyGoods'];//取出访问的ID值 $arr=explode(',', $str ); for ($row = 0; $row < 3; $row++) { $medium_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($arr[$row]), 'full');//缩略图 echo ' <li> <div class="pic" target="_blank" style=" background-image:url('.$medium_image_url[0].')"> </div> <div class="t_1"><a href="/?p='.$arr[$row].'">'.get_post($arr[$row])->post_title.'</a></div> <div class="t_2">'.get_post_meta($arr[$row],'_id_oehaoma',true).'</div>'; } ?> |
浅唱6(微信/QQ号:909912499),欢迎分享本文,转载请保留出处!部分内容来自网络,如有侵权请联系删除处理!
相关信息
本站提供代码修改,dedecms,WordPress仿站二次开发 / PHP网站建设以及SEO优化等网络营销推广等服务。
如有需要请加QQ: 909912499