Home  >  Article  >  Backend Development  >  用PHP生成自己的LOG文件_PHP

用PHP生成自己的LOG文件_PHP

WBOY
WBOYOriginal
2016-06-01 12:32:16897browse

如果你的服务器不容许你读他的LOG文件,那你只好停下来分析你的访问者?做你自己的LOG文件吧!
  你仅需做的事情是用PHP计算实际的点击次数,没有错误,没有象'304 Not Modified' 和 'Internal Server Error' 一样的错误。你的代码将生成自己的LOG文件。



/* 用户定义变量 */
$logfile = "clf.log"; /*LOG文件写到那里 */
$timezone = "+0100"; /* Timezone correction */
$lookup_size = true; /* 设置文件的权限 */
$document_root = "/usr/local/apache/share/htdocs";

/* 他可能或不可能对相同的客户端记数
* 一定要对$document_root 这个变量进行设置才可以工作
*/

function write_to_log($str) {
if($fd = @fopen($GLOBALS[ "logfile"], "a")) {
fputs($fd, $str);
fclose($fd);
}
}

function get_var($name,$default) {
if($var = getenv($name)) {
return $var;
} else {
return $default;
}
}

if($remote_host = get_var( "REMOTE_HOST", false)) {
$remote_host = get_var( "REMOTE_ADDR", "-");
}
$remote_user = get_var( "REMOTE_USER", "-");
$remote_ident = get_var( "REMOTE_IDENT", "-");
$server_port = get_var( "SERVER_PORT", 80);
if($server_port!=80) {
$server_port = ":" . $server_port;
} else {
$server_port = "";
}
$server_name = get_var( "SERVER_NAME", "-");
$request_method = get_var( "REQUEST_METHOD", "GET");
$request_uri = get_var( "REQUEST_URI", "");
$user_agent = get_var( "HTTP_USER_AGENT", "");
if($lookup_size == true && $document_root) {
$filename = ereg_replace( "\?.*", "", $request_uri);
$filename = "$document_root$filename";
if(!$size = filesize($filename)) {
$size = 0;
}
} else {
$size = 0;
}

$date = gmdate( "d/M/Y:H:i:s");
$log = "$remote_host $remote_ident $remote_user [$date $timezone] \"".
"$request_method http://$server_name$server_port$request_uri\" 200 $size\n";

write_to_log($log);
?>

摘自:http://www.programfan.com/showarticle.asp?id=683

Statement:
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