A script I use to catch the distinct search queries people use to reach my pages. It's using the HTTP referer value.
<?php
/*
Example:
$url = 'http://www.google.dk/search?q=test';
//You should use $_SERVER['HTTP_REFERER'] above!
$referalDomain = new referalDomain();
$referalDomain->byUrl($url);
if($referalDomain->isSearchEngine())
{
echo $referalDomain->getPhrase($url);
}
*/
class referalDomain
{
public $domain = '';
function byDomain($domain)
{
$this->domain = $domain;
}
function byUrl($url)
{
$array = parse_url($url);
$pattern = '/[a-z0-9\-]*(\.co|\.ac|\.org|\.com|[a-z0-9\-]*)\.[a-z]*$/';
preg_match($pattern,$array['host'],$domain);
$this->domain = $domain[0];
}
function isSearchEngine()
{
$a = $this->getSearchEngines();
if($a[$this->domain]!="")
{
return true;
}
else
{
return false;
}
}
function getSearchEngines()
{
//Key is domain name and value is the regexp-formated get-var used to store queries!
return array('google.co.uk'=>'q',
'google.dk'=>'q',
'google.com'=>'q',
'google.ca'=>'q',
'google.com.au'=>'q',
'google.co.za'=>'q',
'google.co.in'=>'q',
'google.co.in'=>'q',
'google.co.nz'=>'q',
'eniro.dk'=>'q',
'myway.com'=>'searchfor',
'msn.com'=>'q',
'msn.co.uk'=>'q',
'tdconline.dk'=>'q',
'search.msn.dk'=>'q',
'yahoo.com'=>'p',
'jubii.dk'=>'query',
'yahoo.co.uk'=>'p');
}
function getPhrase($url)
{
if($this->isSearchEngine())
{
$engines = $this->getSearchEngines();
$getVar = $engines[$this->domain];
$urlArray = parse_url($url);
$query = $urlArray['query'];
$pattern = '/(^|\&)'.$getVar.'\=([^$\&]*)/';//.'\=.*&|$/';
preg_match($pattern,$query,$matches);
//echo '<b>'.$matches[2].'</b><br>';
//$phrase = urldecode($matches[2]);
//echo '<b>'.$phrase.'</b><br>';
$query = $phrase;
$query = str_replace('æ','æ',$query);
$query = str_replace('ø','ø',$query);
$query = str_replace('Ã¥','å',$query);
$query = str_replace('Æ','Æ',$query);
$query = str_replace('Ø','ø',$query);
$query = str_replace('Ã…','å',$query);
//echo '<b>'.$query.'</b><br>';
//$query = utf8_decode($query);
$phrase = $query;
echo $phrase.'<br>';
return $phrase;
}
return '';
}
}
?>