Source code in LogReferrer.php

To see how this is used return to this tutorial index.

// How do people know to come to these pages, ie what links to these pages ?

// Copyright (c) 2005, Parliament Hill Computers ( Author: Alain D D Williams (
// You may used this file as the basis your own (or organisation's/company's) project (under whatever
// licence that you see fit), but may not claim ownership or copyright of the substantially unmodified file.
// This file is made available in the hope that it is useful, there is no warranty at all, use at your own risk.

// Log the referrer.
// $logmsg will be appended to the message if it is set/not_empty
// Don't log if the referrer is unknown or is from this site.
function LogReferrer($logmsg = '') {
	if( ! array_key_exists('HTTP_REFERER', $_SERVER))

	// Don't log self referrals:
	if( ! array_key_exists('SERVER_NAME', $_SERVER) or preg_match("=^http://" . preg_quote($_SERVER['SERVER_NAME'], '=') .
			"(/|$)=", $_SERVER['HTTP_REFERER']))

	// The log files for each virtual site - must be writable by apache (or whatever your web server runs as).
	// WARNING on the example below: On a redhat machine the path /var/log/httpd cannot be accessed in any way by the user apache,
	// if/when an updating rpm is instelled, the permission of the directory will be reset -- take care.
	// Customise this for your site:
	$LogInfo = array(
		'' => '/var/log/httpd/example/referrer_log',

	// Can't do anything if we don't know about it (probably a config error):
	if( ! array_key_exists($_SERVER['SERVER_NAME'], $LogInfo))

	// If we can't open the file -- do nowt.
	if($log = @fopen($LogInfo[$_SERVER['SERVER_NAME']], 'a')) {
		fwrite($log, date('Y/m/d H:i:s') . " " . $_SERVER['REMOTE_ADDR'] . " " . $_SERVER['REQUEST_URI'] . " " . $_SERVER['HTTP_REFERER'] .
			((string)$logmsg != '' ? " $logmsg" : '') . "\n");

// end

Return to this tutorial index.