h1

Displaying Referrers

March 23rd, 2002

That other, somewhat better known blogging Brent guy posted a neat article about how he does his referrer list with PHP/mySQL.

I’ve been tracking my referrers out of my Apache logs. It’s the 11th field in each log line, so I use this awk one-liner to do it:

awk ‘{a[$11]++}END{for(e in a)print a[e] ” ” e}’ access_log | grep -v blogchat.com | sort +1

(my ISP leaves two logs in my logs dir – the current one, and one for yesterday)

This translates to:

“For each line, look up the referrer from the 11th field, take an associative array element by that name and increment it. At the end of the file, loop through the elements in the array, giving their counts and the referrer name, then filter out any refs from my own site and sort all of this by referrer.”

This is great – does just what I want. But then I have to cut and paste from the commandline to follow those links.

So I wrapped it all in PHP:

(update: added unset($a) to clear array between days)


<html><head>
<link rel=”stylesheet” href=”styles-site.css” type=”text/css” />
</head><body>
<?php
$logfile = “/path/to/my/access_log”;
$cmd = “awk ‘{a[$11]++}END{for(e in a)print a[e] ” ” e}’ $logfile | grep -v blogchat.com | sort +1″;
print “<h4>Today:</h4>\n”;
exec( $cmd, $a, $ret );
for( $i=0; $i < count($a); $i++ ){
   print ereg_replace(“(“.*”)”,”<a target=”_blank” href=\1>\1</a>”, $a[$i]) . “<br />\n”;
}
unset($a);
$logfile = “/path/to/my/access_log.1″;
$cmd = “awk ‘{a[$11]++}END{for(e in a)print a[e] ” ” e}’ $logfile | grep -v blogchat.com | sort +1″;
print “<h4>Yesterday:</h4>\n”;
exec( $cmd, $a, $ret );
for( $i=0; $i < count($a); $i++ ){
   print ereg_replace(“(“.*”)”,”<a target=”blank” href=\1>\1</a>”, $a[$i]) . “<br />\n”;
}
?>
</body></html>

Here’s the result.

There’s always a different way to do everything!

4 comments to “Displaying Referrers”

  1. psst! you mean <html><head>, right? :)


  2. oops – fixed, thanks.


  3. Any easy way of displaying “Google search: search term” rather than “http://www.google.com/search?hl=en&q=search+term&btnG=Google+Search“?


  4. Very neat…