访问日志统计_PHP

WBOY
Release: 2016-06-01 12:30:23
Original
1254 people have browsed it

/*
程序:访问日志统计
作者:放弃思考 (QQ:376123224)
日期:2005-7-22
*/

##################################
###############函数部分###########

function Format2UnixTime ($str)
{
    /*
    该函数为格式转换函数,将类似的:$str = "03/Mar/2005:16:53:32"转化为相应的UNIX时间戳。
    */
    $time = $str;
    $time = str_replace("/"," ",$time);
    $time_array = explode( ":",$time,2);
    $time = $time_array[0]." ".$time_array[1];
    return strtotime($time);
}

function CompareByTimes ($x,$y)
{
    if ( $x[0] == $y[0] )
        return 0;
    else if ($x[0] > $y[0])
        return -1;
    else
        return 1;
}

function CompareByAccessTime ($x,$y)
{
    $x[1] = Format2UnixTime($x[1]);  //先格式化为UNIX时间戳
    $y[1] = Format2UnixTime($y[1]); //先格式化为UNIX时间戳

    if ( $x[1] == $y[1] )
        return 0;
    else if ($x[1] > $y[1])
        return -1;
    else
        return 1;
}

/*主函数GetAccessByLog
$filename为文件路径,
$order为排序方式:
             0:按访问次数排序(缺省值)
      1:按最近一次访问的时间排序。
*/
function GetAccessByLog ($filename,$order=0)
{
    if ( file_exists($filename) )
    {
        $handle = fopen ($filename, "r");
        $ip_times = array();
        while (!feof ($handle)) {
            $buffer = fgets($handle, 999);
            if ((preg_match("#\d{1,2}\/\w{1,3}\/\d{1,4}\:\d{1,2}\:\d{1,2}\:\d{1,2}#",$buffer,$access_time)) && (preg_match("#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#",$buffer,$ip)))
                {
                $ip = $ip[0];
                $access_time = $access_time[0];
                if ( in_array($ip,array_keys($ip_times)))
                    {
                    $ip_times[$ip][0]++;   // $ip_times[$ip][0]为访问次数times
                    $ip_times[$ip][1]=$access_time;   //$ip_times[$ip][1]为访问时间access_time
                    }else
                        {
                        $ip_times[$ip][0] = 1;
                        $ip_times[$ip][1]=$access_time;
                        }
                }
            }
            fclose ($handle);
    }else
    {
        echo "The log file does not exist.";
        exit;
    }

    if ( $order==1 )
    {
        $compare = "CompareByAccessTime";    //按最近一次访问时间排序
        $title = "按最近一次访问时间排序";
    }else{
        $compare = "CompareByTimes";             //按访问次数排序
        $title = "按访问次数排序";
    }
     uasort( $ip_times, $compare );  
     
     echo "

".$title."
";
     foreach (  $ip_times as $ip=>$value )
         {
         echo "IP:".$ip."
访问次数:".$value[0].",最近一次访问时间是:".$value[1]."

";
         }

}

###################################
###################################

###########  Example  ################

$filename = "C:/Apache2/logs/access.log";
GetAccessByLog($filename,0);
// 参数二意义   0:按访问次数排序(缺省值);1:按最近一次访问的时间排序。
    
?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template