The HTTP_REFERER is not something you can depend on being set, or being set correctly. The best solution I can think of is to use sessions and track the user's path through your site via a variable there. Or you could even add values to an array if you want to keep a realtime "log" of sorts:
<?php
session_start();
// get last page if applicable:
$referer = (isset($_SESSION['history'])) ?
$_SESSION['history'][count($_SESSION['history'])-1] :
NULL;
// add current page to history:
$_SESSION['history'][] = $_SERVER['PHP_SELF'];
// user referer value:
if($referer != '/index.php')
{
header("Location: http://www.yoursite.com/index.php");
exit;
}
// otherwise go ahead with this page's processing....
?>