<?php Class AA{ //判断有无文件 没有 则 所有店铺的pv uv 更新成0 并创建 更改单个商户 pv uv+1
function bb{ if (!file_exists('pv.txt')) { // echo 1;die; $shop_id=M('shop')->field('id')->select(); $shop_id_str = ''; foreach($shop_id as $k=>$v){ $shop_id_str.=$v['id']; $shop_id_str.=','; } $shop_id_str = substr($shop_id_str,0,-1); $where_id['id'] = ['in',$shop_id_str];
//第二天 统一改为0 M('shop')->where($where_id)->save(['day_pv'=>0,'day_uv'=>0]); M('shop')->where(['id'=>$shop['id']])->save(['day_uv'=>1,'day_pv'=>1]); //需要将文本入库 $ip = $this->get_ip(); $txt = '-'.$shop['id'].'-'.$ip.'-'.date('Ymd'); file_put_contents('pv.txt',$txt); } else { //获取ip $ip=$this->get_ip(); //拼接查找的字符串 $str='-'.$shop['id'].'-'.$ip.'-'.date('Ymd'); //读取内容 $contents=file_get_contents('pv.txt'); $find_status=strpos($contents,$str); if ($find_status === false) { file_put_contents('pv.txt',$str.PHP_EOL, FILE_APPEND); M('shop')->where(['id'=>$shop['id']])->setInc('day_pv',1); M('shop')->where(['id'=>$shop['id']])->setInc('day_uv',1); } else { M('shop')->where(['id'=>$shop['id']])->setInc('day_pv',1); } }
}
public function get_ip()
{
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return($ip);
}
}
定时任务 0 0 * * * rm -rf 文件 定时删除文件
思路 : 存文件 读文件 找字符 判断操作数据库 每天定时删文件 方法特点 :减少了读取数据库的次数 减少了数据库的压力 适用于日pv uv统计
//本人原创谢谢支持