码迷,mamicode.com
首页 > Web开发 > 详细

php 处理网站访问日志

时间:2021-04-30 12:11:16      阅读:0      评论:0      收藏:0      [点我收藏+]

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

<?php
$r = tail(‘dd.ddmap.log‘,‘Baiduspider‘);
echo ‘<pre>‘;
print_r($r);
echo ‘</pre>‘;
/**
 * @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<ip>[0-9.]+) - - \[(?P<time>[^\]]+)\]+ "GET (?P<url>[^ ]+) HTTP\/1.[1|0|2]" (?P<status>[0-9.]+) (?P<size>[0-9.]+) /i‘;

    preg_match($pattern, $log, $match);
    if(empty($match))return;
    $req[‘http‘] = $log;
    $req[‘reptile‘] = isCon($log,$type);
    if($type !== ‘false‘ && !$req[‘reptile‘]){
        return ‘‘;
    }

    $req[‘ip‘]  = $match[‘ip‘];
    $req[‘time‘]   = $match[‘time‘];
    $req[‘status‘] = $match[‘status‘];


    return $req;
}

 

php 处理网站访问日志

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!