Created
June 25, 2026 10:10
-
-
Save adamzero1/cf423eac388dda10673a64ca2c3abffa to your computer and use it in GitHub Desktop.
Get Query Strings Report From Nginx Log File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env php | |
| <?php | |
| if ($argc < 2) { | |
| echo "Usage: php query_param_report.php <log-file-path>\n"; | |
| exit(1); | |
| } | |
| $filePath = $argv[1]; | |
| if (!file_exists($filePath)) { | |
| echo "Error: File not found: $filePath\n"; | |
| exit(1); | |
| } | |
| if (!is_readable($filePath)) { | |
| echo "Error: File is not readable: $filePath\n"; | |
| exit(1); | |
| } | |
| // Structure: ['param' => ['value' => count]] | |
| $params = []; | |
| $handle = fopen($filePath, 'r'); | |
| if ($handle === false) { | |
| echo "Error: Could not open file: $filePath\n"; | |
| exit(1); | |
| } | |
| $lineCount = 0; | |
| $matchCount = 0; | |
| while (($line = fgets($handle)) !== false) { | |
| $lineCount++; | |
| // Extract the request URI from the log line (e.g. GET /path?foo=bar HTTP/1.1) | |
| if (!preg_match('/"(?:GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS) ([^ ]+) HTTP/', $line, $matches)) { | |
| continue; | |
| } | |
| $uri = $matches[1]; | |
| $queryString = parse_url($uri, PHP_URL_QUERY); | |
| if (empty($queryString)) { | |
| continue; | |
| } | |
| $matchCount++; | |
| parse_str($queryString, $queryParams); | |
| foreach ($queryParams as $key => $value) { | |
| // Flatten arrays (e.g. foo[]=a&foo[]=b) into comma-joined string for display | |
| if (is_array($value)) { | |
| $flat = []; | |
| array_walk_recursive($value, function ($v) use (&$flat) { $flat[] = (string) $v; }); | |
| $value = implode(',', $flat); | |
| } | |
| $value = (string) $value; | |
| if (!isset($params[$key])) { | |
| $params[$key] = []; | |
| } | |
| if (!isset($params[$key][$value])) { | |
| $params[$key][$value] = 0; | |
| } | |
| $params[$key][$value]++; | |
| } | |
| } | |
| fclose($handle); | |
| if (empty($params)) { | |
| echo "No query parameters found in $filePath\n"; | |
| exit(0); | |
| } | |
| // Sort params by total occurrences descending | |
| $paramTotals = []; | |
| foreach ($params as $key => $values) { | |
| $paramTotals[$key] = array_sum($values); | |
| } | |
| arsort($paramTotals); | |
| echo str_repeat('=', 72) . "\n"; | |
| echo "Query Parameter Report\n"; | |
| echo "File: $filePath\n"; | |
| echo "Lines processed: $lineCount | Requests with query strings: $matchCount\n"; | |
| echo str_repeat('=', 72) . "\n\n"; | |
| foreach ($paramTotals as $key => $total) { | |
| $values = $params[$key]; | |
| // Sort values by count descending | |
| arsort($values); | |
| echo str_repeat('-', 72) . "\n"; | |
| echo sprintf("Parameter: %-40s Total occurrences: %d\n", $key, $total); | |
| echo str_repeat('-', 72) . "\n"; | |
| foreach ($values as $value => $count) { | |
| $displayValue = $value === '' ? '(empty)' : $value; | |
| echo sprintf(" %-50s %6d\n", $displayValue, $count); | |
| } | |
| echo "\n"; | |
| } | |
| echo str_repeat('=', 72) . "\n"; | |
| echo sprintf("Total unique parameters: %d\n", count($params)); | |
| echo str_repeat('=', 72) . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment