Skip to content

Instantly share code, notes, and snippets.

@himel-ceps
Created September 9, 2014 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save himel-ceps/38432832a19e8fb72b2c to your computer and use it in GitHub Desktop.
Save himel-ceps/38432832a19e8fb72b2c to your computer and use it in GitHub Desktop.
<?php
if (isset($_POST['submit'])) {
if (!isset($_FILES['access_logs']) || $_FILES['access_logs']['error'] != 0) {
echo '<strong>No file uploaded or file is corrupted.</strong>';
} else {
if ($_FILES['access_logs']['type'] != 'application/octet-stream') {
echo '<strong>Please, upload a valid log file.</strong>';
} else {
$table_display = true;
if (file_exists('upload/apache.log')) {
unlink('upload/apache.log');
}
if (!file_exists('upload')) {
mkdir('upload');
}
move_uploaded_file($_FILES['access_logs']['tmp_name'],'upload/apache.log');
$file_content = file_get_contents('upload/apache.log');
$records = preg_split('/\n/',$file_content);
if (count($records) > 0) {
$hit_count_per_day = array();
$user_count_per_day = array();
$hit_count_per_user = array();
foreach ($records as $row) {
if ($row != '') {
$segments = preg_split('/\[(.+)\]/',$row,-1,PREG_SPLIT_DELIM_CAPTURE);
$date = date('d F, Y',strtotime($segments[1]));
$segments = preg_split('/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/',$row,-1,PREG_SPLIT_DELIM_CAPTURE);
$ip = $segments[1];
$segments = preg_split('/\"(.[^\"]+)\"/',$row,-1,PREG_SPLIT_DELIM_CAPTURE);
$browser = $segments[count($segments) - 2];
$user = $ip . ' ' . $browser;
if (!array_key_exists($date,$hit_count_per_day)) {
$hit_count_per_day[$date] = 1;
} else {
$hit_count_per_day[$date]++;
}
if (!array_key_exists($date,$user_count_per_day)) {
$user_count_per_day[$date][] = $user;
} else {
if (!in_array($user,$user_count_per_day[$date])) {
$user_count_per_day[$date][] = $user;
}
}
if (!array_key_exists($user,$hit_count_per_user)) {
$hit_count_per_user[$user] = 1;
} else {
$hit_count_per_user[$user]++;
}
}
}
}
}
}
}
?>
<html>
<head>
<title>Custom Reports On Apache Access Logs</title>
</head>
<body>
<form method="post" action="index.php" enctype="multipart/form-data">
<input type="file" name="access_logs" />
<input type="submit" name="submit" value="Get Report" />
</form>
<?php if (isset($table_display)) { ?>
<table width="50%" cellpadding="5" cellspacing="0" border="1">
<tr>
<th width="50%">Average Hit / Visitor</th>
<th width="50%"><?php echo round(array_sum($hit_count_per_user) / count($hit_count_per_user),2); ?></th>
</tr>
</table>
<br/>
<table width="50%" cellpadding="5" cellspacing="0" border="1">
<tr>
<th width="50%">Date</th>
<th width="25%">Hits</th>
<th width="25%">Unique Visitors</th>
</tr>
<?php foreach ($hit_count_per_day as $key => $value) { ?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $value; ?></td>
<td><?php echo count($user_count_per_day[$key]); ?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment