<?php
Access log Write class @author Wang Wei 2011.12.14
Class log{
Project and Path
Private $root _path;
Log file Absolute path
private static $log _file_path;
function __construct ($root _path) {
$this->root_path = $root _path. Directory_separator;
Self:: $log _file_path = $this->root_path. ' LogFile '. Directory_separator. Date (' Y_m_d ', Time ()). Log ';
}
Get the visitor's IP address
private static function Getrealip () {
$ip = false;
if (!empty ($_server["Http_client_ip")) {
$ip = $_server["Http_client_ip"];
}
if (!empty ($_server[' http_x_forwarded_for ')) {
$ips = Explode (",", $_server[' http_x_forwarded_for ');
if ($IP) {
Array_unshift ($ips, $IP);
$ip = false;
}
for ($i = 0; $i < count ($ips); $i + +) {
if (!eregi ("^ (10|172\.16|192\.168) \.", $ips [$i])) {
$ip = $ips [$i];
Break
}
}
}
Return ($ip $ip: $_server[' remote_addr ');
}
Write access Log
Public Function Writelog () {
The host name of the user browsing the current page
$uhost = (isset ($_server[' remote_host '))? $_server[' Remote_host ']: '-');
Request time
$rtime = Date (' y-m-d h:i:s ', $_server[' request_time ');
Request type
$rmethod = $_server[' Request_method ');
Request route
$ref = (isset ($_server[' http_referer '))? $_server[' Http_referer ']: '-');
Request protocol Type
$rprotocol = $_server[' Server_protocol ');
Requesting the browser's user browser information
$ragent = Empty ($_server[' http_user_agent ')? ' Unknown ': $_server[' http_user_agent ';
$durl = $_server[' Request_uri ');
$LOGSTR = Self::getrealip (). ' '. $uhost. ' '. $rtime. ' "'. $rmethod. ' '. $ref. '. $rprotocol. ' " '. $ragent. ' '. $durl. ' \ r \ n ";
if (!file_exists ($this->root_path. ' LogFile ')) {
mkdir ($this->root_path. ' LogFile ');
}
$flog = fopen (self:: $log _file_path, ' a ');
Fwrite ($flog, $LOGSTR);
Fclose ($flog);
}
}
?>
<?php
Instructions for use
In the new log class, pass in the absolute path of the directory where you want to generate the log file (note: no End "/")
The logfile directory is created in this directory, and a daily name is generated to generate an access log file by date
$log = new log (the absolute path of the directory where you want to generate the log file);
$log->writelog ();
?>
Homemade PHP Log class that mimics Apache access log file format