Skip to content

Instantly share code, notes, and snippets.

@mpchadwick
Created July 3, 2016 19:24
Show Gist options
  • Save mpchadwick/6c2313eda1ec6d42c8b97ed70fc5a55f to your computer and use it in GitHub Desktop.
Save mpchadwick/6c2313eda1ec6d42c8b97ed70fc5a55f to your computer and use it in GitHub Desktop.
query-string-usage-report
<?php
$handle = fopen("ssl_access_log", "r");
$i = 0;
$reportParamCount = [];
$reportParamParts = [];
$giveFeedbackEvery = 1000;
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
$parts = explode(" ", $buffer);
// Only GET requests
if ($parts[5] !== "\"GET") {
continue;
}
// Skip URLs that match...
if (preg_match('/\.js|\.css|\.jpeg|\.jpg|\.svg|\.png|\.gif|status\.php/', $parts[6])) {
continue;
}
$i++;
$pos = strpos($parts[6], '?');
// There was no query string...
if ($pos === false) {
$reportParamCount[0]++;
continue;
}
$queryString = substr($parts[6], $pos + 1);
$params = explode('&', $queryString);
$reportParamCount[count($params)]++;
foreach ($params as $param) {
$paramParts = explode('=', $param);
$reportParamParts[$paramParts[0]]['usageCount']++;
$reportParamParts[$paramParts[0]]['values'][$paramParts[1]]++;
}
if ($i % $giveFeedbackEvery === 0) {
echo 'Analyzed ' . $i . ' lines' . PHP_EOL;
}
}
fclose($handle);
ksort($reportParamCount);
uasort($reportParamParts, function ($a, $b) {
if ($a['usageCount'] === $b['usageCount']) {
return 0;
}
return $a['usageCount'] > $b['usageCount'] ? -1 : 1;
});
echo '-----------------' . PHP_EOL;
echo 'Report' . PHP_EOL;
echo '-----------------' . PHP_EOL;
echo 'Param Count Report'. PHP_EOL;
foreach ($reportParamCount as $key => $val) {
echo $key . ': ' . $val . PHP_EOL;
}
echo 'Param Usage Report' . PHP_EOL;
foreach ($reportParamParts as $key => $val) {
echo '> ' . $key . PHP_EOL;
echo '>>> Used ' . $val['usageCount'] . ' times' . PHP_EOL;
echo '>>> ' . count($val['values']) . ' unique values' . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment