Main Content

What brings people to my web site?

Archive - Originally posted on "The Horse's Mouth" - 2005-07-13 06:16:14 - Graham Ellis

How busy is your web site? How do people find it? Where do they arrive from? If you have access to your log files and they're using the "combined" format that tells you about the referer, you'll have that information - but hidden deeply in what's likely to be a huge file.

Here's a little bit of PHP code that you could put at the top of a page that you're particularly interested in tracking; it'll let you keep a log of how many visits you get from where, maintaining a very simple data file that you can look at from time to time, and reset when you want to start counting again.


<?php

/* Page to report keep tabs of all the various referers */

/* Find the referer */

$whence = $_SERVER[HTTP_REFERER];
if (! $whence) $whence = "-";

$previous = file("reflist.txt");
$repeat = 0;

// If this is a previously know referer, add 1 to the count

for ($k=0; $k $wl = explode(" ",$previous[$k]);
if ($wl[0] == $whence) {
$ncount = $wl[1] + 1;
$previous[$k] = "$whence $ncount\n";
$repeat = 1;
}
}

// If this is a new referer, add it to the know list

if (! $repeat) {
array_push($previous,"$whence 1\n");
$ncount = 1;
}

// Save the new counts

$fh = fopen("reflist.txt","w");
fputs ($fh,implode("",$previous));
fclose($fh);

// Send out the response page (sample)

?>

<body>
<h1>Headline information
This is visit number <?= $ncount ?> from <?= $whence ?><br>
<a href=/demo/fromwhere.php>Reload linking from here</a><br><br>
<h2>Full information ...</h2>
<?= join("<br>",$previous) ?>
</body>


You may Access the demo from here