? Php/* Program: access log statistics author: give up thinking (QQ: 376123224) date: 2005-7-22 */##################################### ########### function ############ functionFormat2UnixTime ($ str) {/* This function is a format conversion function, which will be similar
/*
Program: access log statistics
Author: give up thinking (QQ: 376123224)
Date: 2005-7-22
*/
##################################
############## Function ###########
Function Format2UnixTime ($ str)
{
/*
This function is a format conversion function that converts a similar: $ str = "03/Mar/2005: 16: 53: 32" to a UNIX timestamp.
*/
$ 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]); // format it as a UNIX timestamp first
$ Y [1] = Format2UnixTime ($ y [1]); // format it as a UNIX timestamp first
If ($ x [1] = $ y [1])
Return 0;
Else if ($ x [1]> $ y [1])
Return-1;
Else
Return 1;
}
/* Main function getaccpolicylog
$ Filename is the file path,
$ Order is the sorting method:
0: Sort by access count (default)
1: sort by the last access time.
*/
Function getaccpolicylog ($ 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] indicates the times of access
$ Ip_times [$ ip] [1] = $ access_time; // $ ip_times [$ ip] [1] indicates the access time 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"; // sort by the last access time
$ Title = "sort by the last access time ";
} Else {
$ Compare = "CompareByTimes"; // Sort by access count
$ Title = "sort by number of visits ";
}
Uasort ($ ip_times, $ compare );
Echo"
". $ Title ." ";
Foreach ($ ip_times as $ ip => $ value)
{
Echo "IP:". $ ip ."
Number of visits: ". $ value [0].", the last access time is: ". $ value [1]."
";
}
}
###################################
###################################
########### Example ################
$ Filename = "C:/Apache2/logs/access. log ";
Getaccpolicylog ($ filename, 0 );
// Value 2: 0: sort by the number of visits (default); 1: sort by the last visit time.
?>