PCHomepage
Programmer
I created a function to emulate the Apache access.log in MySQL and it is working but there are several things with which I need help. Any thoughts on the following?
The first is $status for which I created the GetStatus() function but when it is used, the server times out. Using the same code directly into AutoLog() it seems to work but not as a function.
Secondly, the GetSize() function seems to work but it is giving only the size of the path and file name, not the page with content. Do I need to use fopen() to actually load it or is there some other way?
Penultimately, the $referer value is using $_SERVER['HTTP_REFERER'], which I understand does not work on many browsers, is doing what is expected: nothing so I'm looking for some some workaround. Actually I thought it should work in Firefox but it does not with the latest version.
Finally, the AutoLog() function is being called from another script that is included into each and every page on the site so it is logging all pages but I cannot think of any other way to call the function to make it trigger on any type of access, such as images, in order to more closely emulate the Apache log entries. This is not of any major importance to me but I am quite curious if there is a way to do it.
The first is $status for which I created the GetStatus() function but when it is used, the server times out. Using the same code directly into AutoLog() it seems to work but not as a function.
Secondly, the GetSize() function seems to work but it is giving only the size of the path and file name, not the page with content. Do I need to use fopen() to actually load it or is there some other way?
Penultimately, the $referer value is using $_SERVER['HTTP_REFERER'], which I understand does not work on many browsers, is doing what is expected: nothing so I'm looking for some some workaround. Actually I thought it should work in Firefox but it does not with the latest version.
Finally, the AutoLog() function is being called from another script that is included into each and every page on the site so it is logging all pages but I cannot think of any other way to call the function to make it trigger on any type of access, such as images, in order to more closely emulate the Apache log entries. This is not of any major importance to me but I am quite curious if there is a way to do it.
PHP:
[COLOR=darkgray]function AutoLog() {
$remote_host = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : "-";
$logname = (isset($_SERVER['REMOTE_HOST'])) ? $_SERVER['REMOTE_HOST'] : "-";
$user = (isset($_SERVER['REMOTE_USER'])) ? $_SERVER['REMOTE_USER'] : "-";
$date_time = $_SERVER['REQUEST_TIME'];
$method = $_SERVER['REQUEST_METHOD'];
$request = $_SERVER['REQUEST_URI'];
$protocol = $_SERVER['SERVER_PROTOCOL'];[/color]
[bold]$status = GetStatus();
$bytes = GetSize();
$referer = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : "";[/bold]
[COLOR=darkgray]$user_agent = $_SERVER['HTTP_USER_AGENT'];
$date_time = date('Y-m-d H:i:s', $date_time);
$sqlInsert = sprintf("INSERT INTO accesslog (RemoteHost, IdentUser, AuthUser, TimeStamp, Method, RequestURI, RequestProtocol, Status, Bytes, Referer, UserAgent)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%u', '%u', '%s', '%s')",
$remote_host,
$logname,
$user,
$date_time,
$method,
$request,
$protocol,
$status,
$bytes,
$referer,
$user_agent);
DBConnect($sqlInsert, "Insert", "db_name");
}[/color]
[bold]function GetStatus() {
$URL = "[URL unfurl="true"]http://$_SERVER[/URL][HTTP_HOST]$_SERVER[REQUEST_URI]";
$headers = get_headers($URL);
return substr($headers[0], 9, 3);
}
function GetSize() {
$QueryString = (isset($_SERVER['QUERY_STRING'])) ? "?".$_SERVER['QUERY_STRING'] : "";
return strlen($_SERVER['SCRIPT_FILENAME'].$QueryString);
}[/bold]