PHP教程

php 处理网站访问日志

本文主要是介绍php 处理网站访问日志,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<?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 处理网站访问日志的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!