web_stats
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
web_stats [2024/05/01 17:57] – created 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 === | ||
+ | ### | ||
+ | The main script is copied over to any web server with PHP support. Drop it into a folder where you are storing your website or web files that you want to monitor. At the very bottom of any pages you want to monitor and before the closing body tag add the following code; | ||
+ | ### | ||
+ | < | ||
+ | <?php include ' | ||
+ | </ | ||
+ | ### | ||
+ | Make any required changes in the config section. Then wait for traffic to hit your website / page. The first time this happens the stats viewer namely viewstats.php will be created in the same directory as your web page files. The second time, the log file namely stats.log will be created and the collected visitor data will be added to the top of the file. Subsequent visits will append the log file from the top and when a maximum number of lines is reached the log file is trimmed to keep the size down. | ||
+ | ### | ||
+ | |||
+ | 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? | ||
+ | ### | ||
+ | |||
+ | === 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.1714586237.txt.gz · Last modified: 2024/05/01 17:57 by admin