Homemade PHP Log class that mimics Apache access log file format

Source: Internet
Author: User
Tags apache access log

<?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.