Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Access.log to MySQL

Access.log to MySQL

Access.log to MySQL

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.


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'];
	$status = GetStatus();
	$bytes = GetSize();
	$referer = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : "";
	$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')",
	DBConnect($sqlInsert, "Insert", "db_name");

function GetStatus() {
	$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);

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close