用PHP生成自己的LOG文件
假如你的服务器不容许你读他的LOG文件,那你只好停下来分析你的访问者?做你自己的LOG文件吧,你仅需做的事情是用PHP计算实际的点击次数,没有错误,没有象'304 Not Modified' 和 'Internal Server Error' 一样的错误,你的代码将生成自己的LOG文件:
- <?php
-
- $logfile = "clf.log";
- $timezone = " 0100";
- $lookup_size = true;
- $document_root = "/usr/local/apache/share/htdocs";
-
-
-
-
-
- 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 $sizen";
-
- write_to_log($log);
- ?>