PCHomepage
Programmer
I've created a relatively simple function to convert the Apache access.log file to MySQL, and the plan is to run it just once (if it doesn't time out!) per site to populate the table, then it won't need to be run again as the site will then keep the table up to date automatically with other programming. This is needed because I do not have direct access to the Apache logs on the server but the hosting company is willing to send me a copy of only mine as a one-shot deal.
However, I cannot figure out how to restrict it from parsing the spider and bot hits. For my purposes, they are irrelevant so how can this be changed to ignore or at least to minimize them so as to keep the database table more manageable in size?
Here is the function:
However, I cannot figure out how to restrict it from parsing the spider and bot hits. For my purposes, they are irrelevant so how can this be changed to ignore or at least to minimize them so as to keep the database table more manageable in size?
Here is the function:
PHP:
function ParseToDatabase($path) {
// Parses the NCSA Combined Log Format lines:
$pattern = '/^([^ ]+) ([^ ]+) ([^ ]+) (\[[^\]]+\]) "(.*) (.*) (.*)" ([0-9\-]+) ([0-9\-]+) "(.*)" "(.*)"$/';
global $output;
if (is_readable($path)) :
$fh = fopen($path,'r') or die($php_errormsg);
while (!feof($fh)) :
$s = fgets($fh);
if (preg_match($pattern,$s,$matches)) :
list($whole_match, $remote_host, $logname, $user, $date_time, $method, $request,
$protocol, $status, $bytes, $referer, $user_agent) = $matches;
endif;
$replacements = array("[","]");
$date_time = str_replace($replacements, "", $date_time);
$date_time = strtotime($date_time);
$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', '%s', '%s', '%s', '%s')",
$remote_host,
$logname,
$user,
$date_time,
$method,
$request,
$protocol,
$status,
$bytes,
$referer,
$user_agent);
DBConnect($sqlInsert, "Insert", "db_name");
endwhile;
fclose($fh);
else :
echo "Cannot access log file!";
endif;
}