Submit new Join Quickr Login
[save][edit]
Q: How to get Google, Yahoo and Bing search engine referal keywords with PHP
A:

This first function (getReferalHost) simply obtains the referal URL and uses the PHP function parse_url() to break things down. Then it's a simple set of 'if' statements to return a user-friendly hostname. So whereas the hostname might be images.google.com, I'm using "strstr" to check whether the string 'google' is in the URL, and so on. It's not foolproof but it's okay.


function getReferalHost()
{
    $refer = parse_url($_SERVER['HTTP_REFERER']);
    $host = $refer['host'];
   
    if(strstr($host,'google'))
    {
        return 'Google';
    }
    elseif(strstr($host,'yahoo'))
    {
        return 'Yahoo';
    }
    elseif(strstr($host,'bing'))
    {
        return 'Bing';
    }
}

This next function (getKeywords) parses the referal URL again (not best practice is it? Shame on me). Then I've got a specific preg_match for each of the different vendors (based on the URLs that each of them uses. Google will format their query as such: http://www.google.com/search?&rls=en&q=seopher+another+keyword so I know that what I'm after starts with '&q=' with the keywords separated by '+'. So the regex used matches that. Yahoo and MSN do exactly the same, just with slightly different syntax - hence the need for the 'if' statement.


function getKeywords()
{
    $refer = parse_url($_SERVER['HTTP_REFERER']);
    $host = $refer['host'];
    $refer = $refer['query'];
   
    if(strstr($host,'google'))
    {
        //do google stuff
        $match = preg_match('/&q=([a-zA-Z0-9+-]+)/',$refer, $output);
        $querystring = $output[0];
        $querystring = str_replace('&q=','',$querystring);
        $keywords = explode('+',$querystring);
        return $keywords;
    }
    elseif(strstr($host,'yahoo'))
    {
        //do yahoo stuff
        $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output);
        $querystring = $output[0];
        $querystring = str_replace('p=','',$querystring);
        $keywords = explode('+',$querystring);
        return $keywords;
       
    }
    elseif(strstr($host,'bing'))
    {
        //do msn stuff
        $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output);
        $querystring = $output[0];
        $querystring = str_replace('q=','',$querystring);
        $keywords = explode('+',$querystring);
        return $keywords;
    }
    else
    {
        //else, who cares
        return false;
    }
}

Really you can add as many different vendors to this function, I'm just focusing on the obvious 3 because they constitute about 98%+ of my search traffic.

This final function is what I'm using to write the recommendations; it takes the keywords used as a parameter and uses MySQL scoring to deliver results... So it takes the search terms and tries to find similar content on the website:


function getSearchHelperList($term, $limit, $currentPageID)
{
    $searchterm = $term;
   
    //a function I use to sanitise all inputs
    $searchterm = $this->cleanInput($searchterm);
   
    /*query that takes the searchterms and scores it against the articles
    it also checks that the title isn't the same as the page we're already on
    because we don't want it showing you a link to your current page*/
    $sql = "SELECT CatID, Title, Content, CleanTitle, UNIX_TIMESTAMP(TIME) AS FORMATED_TIME, MATCH (Title, Content) AGAINST ('$searchterm') AS score FROM Articles WHERE MATCH (Title, Content) AGAINST ('$searchterm') AND CleanTitle != '$currentPageID' AND Time < CURRENT_TIMESTAMP ORDER BY score DESC";
   
    $result = mysql_query($sql);
   
    $num = mysql_numrows($result);
   
    if($num < $limit)
    {
        $limit = $num;
    }
   
    //old school right?  for loops!
    //build an unordered list of related content
    if($limit > 0)
    {
        echo '<ul>';
        for($i=0; $i<$limit; $i++)
        {
            echo '<li><a href="/articles/'.mysql_result($result,$i,'CleanTitle').'">';
            echo mysql_result($result,$i,'Title');
            echo '</a></li>';
        }
        echo '</ul>';
   
    }
    else
    {
       //display a message
    }
}

Source: http://www.stevenyork.com/tutorial/how_to_get_search_engine_referal_keywords_
Loading...
Different wording for this problem: [improve wording]
How to get Google, Yahoo search engine referal keywords with PHP
How to get Google, Yahoo, Bing search engine referal keywords with PHP
PHP function to get search engine referral keywords
Processing referral url with php to get search keywords
php get keywords from url
about quickr