Jump to content

Recommended Posts

Posted

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 SITES

  • Learn 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 SITES

  • Learn 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 USE

  • Learn 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.

Posted

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.

Posted

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);

?>

 

 

Posted

Oooh, thanks Byron! Hadn't looked in file_gets/puts_contents() yet but that's certainly more intuitive.

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...