Cool Referring URL Script Not Working

The below is a awesome script I've put together with a coding friend used for tracking form submissions. If the person came from a Google, MSN, or Yahoo search, it inserts the searched keywords. If the person came from another source, it inserts the URL.
Whenever I test it I look in the source code and it inserts properly! But 8 of 9 submissions since I put it up have returned "No search term or referring URL", which should only be the case if they type in the url directly or bookmark the site - extremely, extremely unlikely.
You can test it and see the site at recruitshop.com.au
<input type="hidden" mandatory="false" validation="" sf_title="Search Term" value="<?php echo ReferringSourceLarge(); ?>">
<?php
function ReferringSourceLarge()
{
$refer = parse_url($_SERVER['HTTP_REFERER']);
$host = $refer['host'];
if($host != 'www.recruitshop.com.au' && $host != 'recruitshop.com.au') {
$querycal = $refer['query'];
$_SESSION['query'] = explode("&", $querycal);
$_SESSION['extrefer'] = $_SERVER['HTTP_REFERER'];
}
if (isset($_SESSION['query'])) {
foreach($_SESSION['query'] as $q)
{
list($key, $value) = explode("=", $q);
$a[$key] = urldecode($value);
}
}
if(!isset($_SESSION['extrefer'])) return 'No search term or referring URL';
if(strstr($host,'google'))
{
$_SESSION['a'] = $a['q'];
return $_SESSION['a'];
}
elseif(strstr($host,'yahoo'))
{
$_SESSION['a'] = $a['p'];
return $_SESSION['a'];
}
elseif(strstr($host,'msn'))
{
$_SESSION['a'] = $a['q'];
return $_SESSION['a'];
}
else if (isset($_SESSION['a']))
{
return $_SESSION['a'];
}
else { return $_SESSION['extrefer']; }
}
?>
Jean Paul a.k.a AdwordsMogul
PHPDevelopers.net - Top of the range PHP developers
⇉ Easy Link Saver - Are you tired of the pain of constantly searching for your affiliate links? ( Chrome extension - FREE )
Jean Paul a.k.a AdwordsMogul
PHPDevelopers.net - Top of the range PHP developers
⇉ Easy Link Saver - Are you tired of the pain of constantly searching for your affiliate links? ( Chrome extension - FREE )
Great Success is built from many little successes!
http://www.timbrownlaw.com - My Wee Part of the World.
http://www.LookingOverMyShoulder.com