Jump to content

Tracking User Patterns With Php


Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?>

 

 

Link to comment
Share on other sites

  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...