Few applications use PHP to parse and read apache log files. Generally, log files are collected through server scripts. However, in special cases, PHP may also need this function, here we will share with you my script. First, Apache access log files are generally stored in: ApacheAccessLogvarloghttpdaccess.
Few applications use PHP to parse and read apache log files. Generally, log files are collected through server scripts. However, in special cases, PHP may also need this function, here we will share with you my scripts. First, Apache Access Log files are generally stored in: Apache access log/var/Log/httpd/Access
Few applications use PHP to parse and read apache log files. Generally, log files are collected through server scripts. However, in special cases, PHP may also need this function, here we will share with you my scripts. First, Apache Access Log files are generally stored in: Apache Access log-/var/Log/httpd/access_log, the log file format must be as follows:
IP address-[server date/time] "GET/path/to/page HTTP request type" HTTP response code HTTP sent to client byte reference client browser
Ip address--Server Date/Time [SPACE] "GET/path/to/page HTTP/Type Request" Success Code Bytes Sent To Client Referer Client Browser
I simply list two rows of data from the server access log file:
123.125.71.83--[30/May/2013: 00: 26: 58 + 0800] "GET/HTTP/1.1" 301 593 "-" "Mozilla/5.0 (compatible; baiduspider/2.0; + http://www.baidu.com/search/spider.html )"
65.55.215.72--[26/May/2013: 11: 35: 12 + 0800] "GET/robots.txt HTTP/1.1" 200 335
The main purpose is to count whether the downloaded files are successful, how many bytes are downloaded, and when the files are downloaded.
The final result is as follows:
The code here is what I do:
Set_time_limit (0 );
Error_reporting (E_ALL );
Ini_set ('display _ errors ', 'on ');
$ Ac_arr = file (DRUPAL_PATH. '/cron/logs/access_log ');
Foreach ($ ac_arr as $ key => $ record ){
$ Records = preg_split ("/([0-9] + \. [0-9] + \. [0-9] + \. [0-9] +)/", $ record,-1, PREG_SPLIT_DELIM_CAPTURE );
$ Ip = $ records [1];
$ Left_str = $ records [2];
// Parse other fields
Preg_match ("/\ [(. +) \]/", $ left_str, $ match );
$ Access_time = $ match [1];
$ Access_unixtime = strtotime ($ access_time );
$ Access_date = date ('Y-m-d', $ access_unixtime );
$ Yesterday_unixtime = strtotime (date ("Y-m-d", time (). "-1 day ");
$ Yesterday_date = date ('Y-m-d', $ yesterday_unixtime );
// The scheduled task only keeps the access logs of the previous day
If ($ yesterday_date! = $ Access_date ){
Continue;
}
$ Left_str = preg_replace ("/^ ([-] *) \ [(. +) \]/", "", $ left_str );
$ Left_str = trim ($ left_str );
Preg_match ("/^ \" [A-Z] {3, 7} (. [^ \ "] +) \"/I ", $ left_str, $ match );
$ Full_path = $ match [0];
$ Http = $ match [1];
$ Link = explode ("", $ http );
$ Uaid = "";
// Counts downloads under a specified access path
If ($ link & preg_match ("/^ \/course \/automation \/MP +/", $ link [0]) {
Preg_match ("/uaid = ([0-9] +)/", $ link [0], $ match );
$ Uaid = $ match [1];
Preg_match ("/^ \/course \/automation \/(MP [0-9] + \. zip)/", $ link [0], $ match );
$ Course = $ match [1];
}
Else {
Continue;
}
$ Left_str = str_replace ($ full_path, "", $ left_str );
$ Left_arr = explode ("", trim ($ left_str ));
Preg_match ("/([0-9] {3})/", $ left_arr [0], $ match );
$ Success_code = $ match [1];
Preg_match ("/([0-9] + \ B)/", $ left_arr [1], $ match );
$ Bytes = $ match [1];
$ Left_str = str_replace ($ success_code, "", $ left_str );
$ Left_str = str_replace ($ bytes, "", $ left_str );
$ Left_str = trim ($ left_str );
Preg_match ("/^ \" (. [^ \ "] +) \"/", $ left_str, $ match );
$ Ref = $ match [1];
$ Left_str = str_replace ($ match [0], "", $ left_str );
Preg_match ("/\" (. [^ \ "] +)/", trim ($ left_str), $ match );
$ Browser = $ match [1];
Print ("
IP: $ ip
Access Time: $ access_time
Page: $ link [0]
Type: $ link [1]
Success Code: $ success_code
Bytes Transferred: $ bytes
Referer: $ ref
Browser: $ browser ");
// Insert into database
// Db_query ("insert into {automation_file_download} (uaid, course, download_date, ip, access_time, page, type, success_code, bytes, referer, browser) VALUES ('% s ', '% s', % d,' % s', '% s', % d, % d,' % s ', '% s') ", $ uaid, $ course, $ access_date, $ ip, $ access_unixtime, $ link [0], $ link [1], $ success_code, $ bytes, $ ref, $ browser );
}
Briefly describe my code:
(...)
Read the rest of PHP parsing Apache access_log (44 words)
©Lixiphp for LixiPHP, 2013. | Permalink | No comment | Add to del. icio. us
Post tags: access_log, Apache, crontab, Linux
Feed enhanced by Better Feed from Ozh