web_stats
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| web_stats [2024/05/01 18:29] – admin | web_stats [2024/05/02 14:21] (current) – admin | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| {{wiki: | {{wiki: | ||
| + | |||
| + | === Requirments === | ||
| + | * FTP or SSH access to a web server with PHP support | ||
| + | * < | ||
| + | * Visitors to your website / page | ||
| === Description / Use === | === Description / Use === | ||
| Line 15: | Line 20: | ||
| ### | ### | ||
| - | So we end up with // | + | So we end up with // |
| ### | ### | ||
| At the bottom of the viewed statistics table are form buttons to either completely clear the log file or to refresh the page / logs. It's suggested that these are removed / commented out of either the viewer or in the html template section of the script if you do not want any uninvited visitors clearing your logs for you? | At the bottom of the viewed statistics table are form buttons to either completely clear the log file or to refresh the page / logs. It's suggested that these are removed / commented out of either the viewer or in the html template section of the script if you do not want any uninvited visitors clearing your logs for you? | ||
| ### | ### | ||
| + | |||
| + | === Demo === | ||
| + | Click [[https:// | ||
| + | |||
| + | === The Code === | ||
| + | < | ||
| + | <?php | ||
| + | /* Simple PHP Web Page Statistics | ||
| + | Log file and viewer created on first run | ||
| + | Call this script by including in web page | ||
| + | <? | ||
| + | |||
| + | // Config Section | ||
| + | define(" | ||
| + | define(" | ||
| + | define(" | ||
| + | define(" | ||
| + | define(" | ||
| + | define(" | ||
| + | define(" | ||
| + | |||
| + | // HTML Template / Style | ||
| + | $statsHtml=' | ||
| + | < | ||
| + | < | ||
| + | table{background:# | ||
| + | td{background:# | ||
| + | th{background:# | ||
| + | input[type=submit]: | ||
| + | -webkit-border-radius: | ||
| + | a:link, visited{text-decoration: | ||
| + | </ | ||
| + | < | ||
| + | <table cellpadding=" | ||
| + | < | ||
| + | <?php include " | ||
| + | </ | ||
| + | <form method=" | ||
| + | <input type=" | ||
| + | <button onclick = " | ||
| + | < | ||
| + | window.localStorage.clear(); | ||
| + | location.reload(); | ||
| + | } </ | ||
| + | </ | ||
| + | <?php | ||
| + | if(isset($_POST[" | ||
| + | {$file = fopen(" | ||
| + | fclose($file); | ||
| + | </ | ||
| + | |||
| + | // Local Data Collection | ||
| + | $date = date(DATE_FORMAT); | ||
| + | $time = date(TIME_FORMAT); | ||
| + | |||
| + | // Get remote IP address / obfuscate own local IP address | ||
| + | $userIp | ||
| + | |||
| + | if ($userIp != (filter_var($userIp, | ||
| + | { $publicIp = " | ||
| + | else | ||
| + | { $publicIp = $userIp; $hostName | ||
| + | |||
| + | // Get Useragent and Browser | ||
| + | $userAgent = (isset($_SERVER[' | ||
| + | $commonBrowsers = [" | ||
| + | $userBrowser = ' | ||
| + | | ||
| + | // Operating System | ||
| + | $platform = ' | ||
| + | if (preg_match('/ | ||
| + | { $platform = ' | ||
| + | elseif (preg_match('/ | ||
| + | { $platform = ' | ||
| + | elseif (preg_match('/ | ||
| + | { $platform = ' | ||
| + | |||
| + | // Users Browser | ||
| + | foreach ($commonBrowsers as $browser) { | ||
| + | if (strpos($userAgent, | ||
| + | } | ||
| + | switch ($userBrowser) { | ||
| + | case ' | ||
| + | case ' | ||
| + | case ' | ||
| + | case ' | ||
| + | } | ||
| + | | ||
| + | // Remote Data Collection via http:// | ||
| + | $api1 = json_decode(file_get_contents(" | ||
| + | $isp = $api1[" | ||
| + | $timezone = $api1[" | ||
| + | $country = $api1[" | ||
| + | |||
| + | // The Log Entry | ||
| + | $logEntry = "< | ||
| + | $hostName</ | ||
| + | | ||
| + | // Create Viewer or Create Entry in Log File | ||
| + | if (!file_exists(STATS_FILE)) { | ||
| + | $htmlWrite = fopen(STATS_FILE," | ||
| + | fwrite($htmlWrite, | ||
| + | } | ||
| + | else { | ||
| + | | ||
| + | } | ||
| + | |||
| + | // Trim logfile to max lines - 2 lines per record | ||
| + | | ||
| + | | ||
| + | if ($lines > (MAX_RECORDS) *2) { | ||
| + | array_pop($logEntries); | ||
| + | array_pop($logEntries); | ||
| + | file_put_contents((LOG_FILE), | ||
| + | } | ||
| + | ?> | ||
| + | </ | ||
| + | |||
| + | |||
| + | The best lightweight self hosted PHP web stats logger in my opinion is ((https:// | ||
| + | |||
web_stats.1714588150.txt.gz · Last modified: by admin
