要求的功能:免注册会员,可以记录用户访问了我站哪些文章,并给出一个文章列表,php方式,thinkphp3演示代码
思路:
用客户的HTTP请求头数据,用md5加密成唯一的字符串,把这个串作为txt的文件名,保存在本地静态目录中,当作cookie使用,除非用户升级系统或浏览器版本信息,否则这个信息不会变化,对于您有限制访客来说,这个字符串几乎是唯一的,两个用户间重复的可能性几乎为0,所以他可 替换cookie,而不必担心用户开启隐私模式,禁用cookie之类的,也不需要用户注册会员,即可以跟踪用户行为.
一,在模板中增加ajax,用get的方式来访问访客浏览记录.
<li id="look_history">
模板上要显示列表的地方
</li>
下面是ajax代码
get的方式访问php后台的一个函数,得到用户的浏览记录
$(function(){
$.ajax({
url: "?a=look_history",
type:'get',
dataType: 'html',
data: '',
success : function(result) {
$("#look_history").html(result);
}
});
});
读取缓存记录方法为.look_history函数代码为
function look_history(){
//浏览记录
$user_agent = $_SERVER['HTTP_USER_AGENT'];
//用客户的HTTP请求头数据,用md5加密成唯一的字符串,这个串保存在本地静态目录中,当作cookie使用,除非用户升级系统或浏览器版本信息,否则这个信息不会变化,对于您有限制访客来说,这个字符串几乎是唯一的,两个用户间重复的可能性几乎为0,所以他可 替换cookie,而不必担心用户开启隐私模式,禁用cookie之类的,也不需要用户注册会员,即可以跟踪用户行为.
$user_agent=md5($user_agent);
//Browsing_History为缓存存放目录,要先建立
$cache=RUNTIME_PATH.'Browsing_History/'.$user_agent.'.txt';
$oldstr=file_get_contents($cache);
$old_arr=unserialize($oldstr);
array_multisort(array_column($old_arr,3),SORT_DESC,$old_arr);//数组排序,按浏览时间倒序排列
$old_arr2=array_slice($old_arr,0,3);
$html='<a target="_blank" href="'.U('user_shou').'">
<i class="img nav_3"></i>
<span>浏览记录</span>
<span class="badge shoSpan">
'.count($old_arr).'
</span>
</a>
<div class="navUl ale" style="display: none; opacity: 1;margin-top:-30px">
<ul class="nav-ul" style="max-height: 490px;">';
$dddd=0;
foreach ($old_arr2 as $k => $v) {
if($dddd<=2){
$html.='<li class="clearfix"> <img src="'.__ROOT__.$v[2].'" class="pull-left navPic"><a href="'.U('Index/cpinfo',array('id'=>$v[0])).'" class="pull-right navA">'.$v[1].'</a></li>';
$dddd++;
}
}
$html.= '</ul></div>';
echo $html;
}
用户访问一个文章时,写入缓存,并删除过期的
$this->del_file_by_time(RUNTIME_PATH.'Browsing_History/',7);//需要自动删除7天以前所有缓存文件
//无需登录,记录浏览足迹
$id=$_GET['id'];
//先写入浏览记录,单条追加
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$user_agent=md5($user_agent);
$cache=RUNTIME_PATH.'Browsing_History/'.$user_agent.'.txt';
//$id 表示用户浏览的文章的id编号 $id=$_GET['id'];
$new_arr[]=array($id,$cont2['title'],$cont2['pic'],time());
$newstr=serialize($new_arr);
if(!is_file($cache)){
file_put_contents($cache,$newstr);//先用php生成缓存.
}else{
//缓存存在,先取出原来的.再查询如果文章的id不同,则追加后再放入
$oldstr=file_get_contents($cache);
$old_arr=unserialize($oldstr);
$iii=0;
foreach($old_arr as $kkk => $value){
if($value[0]==$id){//如果他浏览过这个文章了,再浏览时,则更新时间
$iii=1;
$old_arr[$kkk][3]=time();
//print_r($old_arr);exit;
$last_str=serialize($old_arr);
file_put_contents($cache,$last_str);//先用php生成缓存.
continue;
}
}
//缓存存在.但是缓存中不存在这个文章的浏览记录,则直接追加
if($iii==0){
$old_arr[]=array($id,$cont2['title'],$cont2['pic'],time());
file_put_contents($cache,serialize($old_arr));//先用php生成缓存
}
}
删除缓存的函数先定义
function del_file_by_time($dir,$n)
{
if(is_dir($dir)){
if($dh=opendir($dir)){
while (false !== ($file = readdir($dh))){
if($file!="." && $file!=".."){
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)){
$filedate=filemtime($fullpath);
$minutes=round((time()-$filedate)/86400);
if($minutes>$n)
unlink($fullpath); //删除文件
}
}
}
}
closedir($dh);
}
}
我们公司的演示网站右侧浏览记录就是了 光纤放大器