Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
4.30/5 (10 votes)
See more:
With this class we try to catch some keywords from google, yahoo and bing search engines.

<?php
class keywords
{
 private $referer;
 private $_e;
 public $keywords;

 public function __construct()
 {
   if($_SERVER['HTTP_REFERER'])
   {
     if(preg_match("#\.google|search\.yahoo|\.bing#", $_SERVER['HTTP_REFERER']))
     {
       $this->referer = urldecode($_SERVER['HTTP_REFERER']);
     }
     else
     {
       return;
     }    
   }
   else
   {
     return;
   }    
 }

 private function getSeparators()
 {
   $this->_e = (preg_match("#\?q=|\?p=#", $this->referer)) ? "\?" : "&";
 }

 public function getKeywords()
 {
   if(!empty($this->referer))
   {
     $this->getSeparators();
     //google
     if(preg_match("#\.google#", $this->referer))
     {
       $m_ = preg_match("#{$this->_e}q=(.+?)&#si", $this->referer, $this->keywords);

       if($m_ == 0)
       {
         return false;
       }
     }
     //yahoo
     elseif(preg_match("#search\.yahoo#", $this->referer))
     {
       $m_ = preg_match("#{$this->_e}p=(.+?)\&#si", $this->referer, $this->keywords);

       if($m_ == 0)
       {
         return false;
       }
     }
     //bing
     elseif(preg_match("#\.bing#", $this->referer))
     {
       $m_ = preg_match("#{$this->_e}q=(.+?)\&#si", $this->referer, $this->keywords);

       if($m_ == 0)
       {
         return false;
       }
     }
     else
     {
       return false;
     }

     return $this->keywords[1];
   }
   else
   {
     return false;
   }
  }
}
?>


Save this script as keywords_class.php

Now let’s try to print these keywords.

<?php
require_once('keywords_class.php');
$keywordsObj = new keywords();
$keys = $keywordsObj->getKeywords();

if($keys)
{
 print $keys;
}
else
{
 print "ooops";
}
?>


Save this code as index.php
Posted

1 solution

There is a bug with your regex.
Say for example that you were refered to by google.uk/search?q=example&ie=utf-8, your code would return 'example'. If, however, you were refered to by google.uk/search?q=example, it would return 'oops'.

The simple fix is to change the limitation variable from & to (&|$).

The following is the testbed I used.
PHP
<?php
class SearchEngineKeywords
{
    private $referer;

    public function __construct( $search_string = null )
    {
        $referer = isset( $search_string ) && is_string( $search_string )
            ? $search_string : ( isset( $_SERVER[ 'HTTP_REFERER' ] )
                ? $_SERVER[ 'HTTP_REFERER' ] : null );

        if ( isset( $referer ) && preg_match( '/\.google|search\.yahoo|\.bing/' , $referer ) )
            $this->referer = urldecode( $referer );
    }

    public function get_keywords()
    {
        if ( !isset( $this->referer ) )
            return false;

        // Google and Bing
        if ( preg_match( '/\.google|\.bing/' , $this->referer ) )
            $get_variable_name = 'q';

        // Yahoo
        elseif( preg_match( '/search\.yahoo/', $this->referer ) )
            $get_variable_name = 'p';

        // Default
        else
            return false;

        return preg_match( '/' . $this->get_separators() . $get_variable_name . '=(.+?)(&|$)/si' , $this->referer , $keywords ) == 0
            ? false : $keywords[1];
    }

    private function get_separators()
    {
        return preg_match( '/\?q=|\?p=/' , $this->referer ) ? '\?' : '&';
    }
}
?>
The previous code was tested by:
PHP
<?php
$SearchEngineKeywords = new SearchEngineKeywords( 'http://www.google.co.uk/search?q=example' );

if ( $keywords = $SearchEngineKeywords->get_keywords() )
{
	echo $keywords;
}
else
{
	echo 'ooops';
}
?>
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900