Shinryuu Posted December 10, 2012 Share Posted December 10, 2012 So I was wondering a useful way to use php the other day and came up with a simple but extremely useful script. Firstly I want to make it clear that this is more for smaller site designers wanting to find out how to best improve their sites, however larger sites with more traffic may find it useful as well for different reasons. USES FOR SMALL SITESLearn how your users move around your site.Learn the most popular areas of your site.Help learn what can be done to move users to the areas of your site that you want.USES FOR LARGE SITESLearn when users are visiting broken links.Learn when users are trying to enter restricted areas.Learn what IP is most likely that of a spammy or otherwise disruptive user.OVERALL USELearn what peak times are for your site.Identify when the best time for site maintenace is.The script is simple and will only create a plaintext log but you can refine it to use MySQL databases for storage. I advise putting the script after the page's html and styling is processed to avoid any major problems with displays being broken. Now to the fun part. Small sites:<?php $ip=$_SERVER['REMOTE_ADDR']; $page=$_SERVER['REQUEST_URI']; $dayassoc=getdate(); $output= $ip."\t".$page."\t".$dayassoc['mon']."/".$dayassoc['mday']."/".$dayassoc['year']."\t".$dayassoc['hours'].":".$dayassoc['minutes']."\r\n"; file_put_contents("tracking.txt", $output, FILE_APPEND); ?>put that on each page to get as detailed a listing as you can about how users interact with your site. You can include this on custom error pages as well and I'll explain that shortly. Large sites:I suggest only placing it on error pages, also replace "errorpagecode" with 401, 404, etc depending on the page you're putting it in. If you must put it in a trafficed area try to limit the amount of pages you paste it in to avoid collisions with file write requests.<?php $errcode="errorpagecode"; $ip=$_SERVER['REMOTE_ADDR']; $page=$_SERVER['REQUEST_URI']; $dayassoc=getdate(); $output= $ip."\t".$page."\t".$dayassoc['mon']."/".$dayassoc['mday']."/".$dayassoc['year']."\t".$dayassoc['hours'].":".$dayassoc['minutes']."\t".$errcode."\r\n"; file_put_contents("tracking.txt", $output, FILE_APPEND); ?> Feel free to leave questions and comments ; EDIT: Changed to use file_puts_contents() instead of messy, unintuitive c-like fopen/fputs/fclose per Byron's suggestion. EDIT 2: I should probably add that you'd want to keep people from accessing this file so in your root folder's .htaccess add something like:AuthUserFile "/home/user/.htpasswds/public_html/passwd" AuthName "Visitor Tracking" AuthType Basic <Files "tracking.txt">; require valid-user </Files>where AuthUserFile is the filepath to a nonpublic directory containing the user=>key pairs required to access the file. AuthName can be anything you want. Don't put the require valid-user directive outside the files tag as that will password protect your entire site. Quote Link to comment Share on other sites More sharing options...
torpex2002 Posted December 11, 2012 Share Posted December 11, 2012 I just use Google analytics. Dump one file into your root directory to confirm you're the webmaster, view all manner of charts, navigation patterns, view your own site with an overlay of what percentage of hits each link gets, see your viewers country, browser, operating system, plus plenty of other features, charts and statistics. Quote Link to comment Share on other sites More sharing options...
Snuupy Posted December 11, 2012 Share Posted December 11, 2012 Statcounter is nice as well. Quote Link to comment Share on other sites More sharing options...
Byron Posted December 12, 2012 Share Posted December 12, 2012 Use file_put_contents() instead like this and there's no need to create the tracking.txt: <?php $errcode = "errorpagecode";$ip = $_SERVER['REMOTE_ADDR'];$page = $_SERVER['REQUEST_URI'];$dayassoc = getdate();$output = $ip."\t".$page."\t".$dayassoc['mon']."/".$dayassoc['mday']."/".$dayassoc['year']."\t".$dayassoc['hours'].":".$dayassoc['minutes']."\t".$errcode."\r\n"; file_put_contents("tracking.txt", $output, FILE_APPEND);?> Quote Link to comment Share on other sites More sharing options...
Shinryuu Posted December 12, 2012 Author Share Posted December 12, 2012 Oooh, thanks Byron! Hadn't looked in file_gets/puts_contents() yet but that's certainly more intuitive. Quote Link to comment Share on other sites More sharing options...
i7Grendel Posted December 30, 2012 Share Posted December 30, 2012 Piwik and Google Analytics can give you much more statistics and are much easier to set up. And both are free. Quote Link to comment Share on other sites More sharing options...
Tjoene Posted December 31, 2012 Share Posted December 31, 2012 To make the code even more simpler, you can use this:date("Y-m-d H:i:s")(see: http://php.net/manua...nction.date.php) instead of$dayassoc=getdate(); $dayassoc['mon']."/".$dayassoc['mday']."/".$dayassoc['year']."\t".$dayassoc['hours'].":".$dayassoc['minutes'] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.