PHPでログファイルを書き込む方法

参考コード:
<?php
$logfile = “siterecord2016.log";
$timezone = “+0100"; /* Timezone correction */
$lookup_size = true;
$document_root = “/usr/local/apache/share/htdocs";
function logFileWrite($str) {
if($fd = @fopen($GLOBALS[ “logfile"], “a")) {
fputs($fd, $str);
fclose($fd);
}
}

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

if($remote_host = getVarValue( “REMOTE_HOST", false)) {
$remote_host = getVarValue( “REMOTE_ADDR", “-“);
}
$remote_user = getVarValue( “REMOTE_USER", “-“);
$remote_ident = getVarValue( “REMOTE_IDENT", “-“);
$server_port = getVarValue( “SERVER_PORT", 80);
if($server_port!=80) {
$server_port = “:" . $server_port;
} else {
$server_port = “";
}
$server_name = getVarValue( “SERVER_NAME", “-“);
$request_method = getVarValue( “REQUEST_METHOD", “GET");
$request_uri = getVarValue( “REQUEST_URI", “");
$user_agent = getVarValue( “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";

logFileWrite($log);
?>

PHP

Posted by arkgame