Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Catching Keywords from Search Engines

, 18 Dec 2009
Rate this:
Please Sign up or sign in to vote.
With this class we try to catch some keywords from google, yahoo and bing search engines.<?phpclass keywords{ private $referer; private $_e; public $keywords; public function __construct() { if($_SERVER['HTTP_REFERER']) { ...
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

License

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

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralThanks Pinmemberjasonp122-Jan-10 16:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 18 Dec 2009
Article Copyright 2009 by caprico.caprico
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid