Home > Article > Backend Development > Generate your own LOG file with PHP_PHP tutorial
If your server doesn't allow you to read its LOG file, then you have to stop and analyze your visitors? Make your own LOG file!
All you have to do is count the actual number of clicks in PHP, no errors, no errors like '304 Not Modified' and 'Internal Server Error'. Your code will generate its own LOG file.
/* User-defined variables*/
$logfile = "clf.log"; /*Where the LOG file is written*/
$timezone = "+0100"; /* Timezone correction */
$lookup_size = true; /* Set file permissions*/
$document_root = "/usr/local/apache/share/htdocs";
/* It may or may not count the same client
* The $document_root variable must be set to work
*/
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);
?>