php 处理网站访问日志

2021-05-29 17:02

阅读:633

标签:seek   slice   fse   one   处理   hone   gets   strpos   div   

php
$r = tail(‘dd.ddmap.log‘,‘Baiduspider‘);
echo
;
print_r($r);
echo
; /** * @param $filename * @param false $num * @param int $n * @param int $base * @return array */ function tail(string $filename,string $type = ‘false‘,int $n = 500 , int $base=5) : array { $fp = fopen($filename,‘r+‘); assert($n>0); $pos = $n+1; // fseek($fp, -0,SEEK_END) ; // $pos = ftell($fp) / 5; $lines = array(); while(count($lines) $n){ try{ fseek($fp,-$pos,SEEK_END); } catch (Exception $e){ fseek(0); break; } $pos *= $base; while(!feof($fp)){ $res = fgets($fp); if(!$res)continue; $res = Hhandle($res,$type); if(!$res){continue;} array_unshift($lines,$res); } } return $lines; return array_slice($lines,0,$n); } /** * @param $str * @return array */ function isCon(string $str,$type) : string { $spiders = [‘Googlebot‘, ‘Baiduspider‘, ‘Sogou‘, ‘YodaoBot‘, ‘YisouSpider‘,‘msnbot‘,‘baiduboxapp‘,‘QQBrowser‘,‘iPhone‘,‘Chrome‘,‘Firefox‘]; $http = [$str]; if($type !== ‘false‘){ if(strpos($str,$type)){ return $type; } return ‘‘; } foreach($spiders as $K=>$v){ // $v = strtolower($v); if(strpos($str,$v)){ return $v; } } return ‘‘; } /** * @param $log * @param false $type * @return array|void */ function Hhandle(string $log, $type=false) { $req = []; $pattern = ‘/^(?P[0-9.]+) - - \[(?P

 

php 处理网站访问日志

标签:seek   slice   fse   one   处理   hone   gets   strpos   div   

原文地址:https://www.cnblogs.com/LF-place/p/14718606.html


评论


亲,登录后才可以留言!