Source Rally PHP Community Scripts .. Sign up .. Login
A script I use to catch the distinct search queries people use to reach my pages. It's using the HTTP referer value.
Access: Public      Tags: google, search queries, referer
Add to favourites       Subscribe comments       Copy code       Bookmark
<?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 
'';
    }
}
?>
Add to favourites       Subscribe comments       Copy code       Bookmark
Sign up to add your own comment here!

Shared by:

regin

Mail user Add to friends
All user contributed content is available under the unless specified otherwise.
Remaining copyrights Regin Gaarsmand 2006-2008
About www.SourceRally.net