Home Backend Development PHP Tutorial 访问日志统计_PHP

访问日志统计_PHP

Jun 01, 2016 pm 12:30 PM
if ip sort log time statistics access

/*
程序:访问日志统计
作者:放弃思考 (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:按最近一次访问的时间排序。
    
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How much does a Douyin level 10 light sign cost? How many days does it take to create a level 10 fan sign? How much does a Douyin level 10 light sign cost? How many days does it take to create a level 10 fan sign? Mar 11, 2024 pm 05:37 PM

On the Douyin platform, many users are eager to obtain level certification, and the level 10 light sign shows the user's influence and recognition on Douyin. This article will delve into the price of Douyin’s level 10 light boards and the time it takes to reach this level to help users better understand the process. 1. How much does a level 10 Douyin light sign cost? The price of Douyin's 10-level light signs will vary depending on market fluctuations and supply and demand. The general price ranges from a few thousand yuan to ten thousand yuan. This price mainly includes the cost of the light sign itself and possible service fees. Users can purchase level 10 light signs through Douyin’s official channels or third-party service agencies, but they should pay attention to legal channels when purchasing to avoid false or fraudulent transactions. 2. How many days does it take to create a level 10 fan sign? Reach level 10 light sign

How to write if in c language to judge multiple conditions How to write if in c language to judge multiple conditions Mar 25, 2024 pm 03:24 PM

In C language, if statement is usually used to execute a specific block of code based on a single condition. However, multiple conditions can be combined to make a determination using logical operators such as &&, ||, and !. Including using logical AND (&&) to judge multiple conditions, using logical OR (||) to judge at least one condition, using logical NOT (!) to judge the negation of a single condition, as well as nesting if statements and using parentheses to clarify priority.

How long does it take to clear the Elden Ring? How long does it take to clear the Elden Ring? Mar 11, 2024 pm 12:50 PM

Players can experience the main plot of the game and collect game achievements when playing in Elden's Circle. Many players don't know how long it takes to clear Elden's Circle. The player's clearance process is 30 hours. How long does it take to clear the Elden Ring? Answer: 30 hours. 1. Although this 30-hour clearance time does not refer to a master-like speed pass, it also omits a lot of processes. 2. If you want to get a better game experience or experience the complete plot, then you will definitely need to spend more time on the duration. 3. If players collect them all, it will take about 100-120 hours. 4. If you only take the main line to brush BOSS, it will take about 50-60 hours. 5. If you want to experience it all: 150 hours of base time.

How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? Mar 24, 2024 pm 01:31 PM

Xiaohongshu, a platform full of life and knowledge sharing, allows more and more creators to express their opinions freely. In order to get more attention and likes on Xiaohongshu, in addition to the quality of content, the time of publishing works is also crucial. So, how to set the time for Xiaohongshu to publish works? 1. How to set the time for publishing works on Xiaohongshu? 1. Understand the active time of users. First, it is necessary to clarify the active time of Xiaohongshu users. Generally speaking, 8 pm to 10 pm and weekend afternoons are the times when user activity is high. However, this time period will also vary depending on factors such as audience group and geography. Therefore, in order to better grasp the active period of users, it is recommended to conduct a more detailed analysis of the behavioral habits of different groups. By understanding users’ lives

How to sort WPS scores How to sort WPS scores Mar 20, 2024 am 11:28 AM

In our work, we often use wps software. There are many ways to process data in wps software, and the functions are also very powerful. We often use functions to find averages, summaries, etc. It can be said that as long as The methods that can be used for statistical data have been prepared for everyone in the WPS software library. Below we will introduce the steps of how to sort the scores in WPS. After reading this, you can learn from the experience. 1. First open the table that needs to be ranked. As shown below. 2. Then enter the formula =rank(B2, B2: B5, 0), and be sure to enter 0. As shown below. 3. After entering the formula, press the F4 key on the computer keyboard. This step is to change the relative reference into an absolute reference.

Detailed explanation of log viewing command in Linux system! Detailed explanation of log viewing command in Linux system! Mar 06, 2024 pm 03:55 PM

In Linux systems, you can use the following command to view the contents of the log file: tail command: The tail command is used to display the content at the end of the log file. It is a common command to view the latest log information. tail [option] [file name] Commonly used options include: -n: Specify the number of lines to be displayed, the default is 10 lines. -f: Monitor the file content in real time and automatically display the new content when the file is updated. Example: tail-n20logfile.txt#Display the last 20 lines of the logfile.txt file tail-flogfile.txt#Monitor the updated content of the logfile.txt file in real time head command: The head command is used to display the beginning of the log file

When will Ark of Destiny Dreams come out? When will Ark of Destiny Dreams come out? Mar 14, 2024 pm 03:00 PM

Many players want to ask when Ark of Destiny Sleepy Dream will be released. Sleepy Dream will meet us on March 13th. There will also be new professional fighter Jia Nan, new continent Rowan, God's Chosen Hero Weapons, new BOSS and other content. Specific details Let’s take a look at the content of this introduction to the launch time of Ark of Destiny Sleepy Dreams. Ark of Destiny Guide: When will Ark of Destiny Dreams be released? Answer: March 13th. Item level requirements. Level 1-Level 3 requires props level: 1540. Level 4 requires props level: 1560. Dropped items: Dream Thoughts, Dream Mark, Falling into despair. Brand new professional fighter male 1. Characteristics: Shura energy, accumulate Shura energy to enter [Kingdom Boxing State] 2. Professional attributes: Melee profession 3. Professional weapon: heavy arm armor. New Continent Rowan 1. How to open it

How to sort in excel How to sort in excel Mar 05, 2024 pm 04:12 PM

Sorting methods in excel: 1. Single column sorting; 2. Multiple column sorting; 3. Custom sorting. Detailed introduction: 1. Single-column sorting is the most common sorting method. It sorts according to a selected column; 2. Multi-column sorting refers to sorting data in multiple columns, usually sorting according to a certain column first. On the basis of, sort by another column; 3. Custom sorting, allowing users to define the sort order according to their own needs.

See all articles