Good day everyone, In this article we will learn how to catch information from “HTTP_REFERER” function such as, referer info, keyword info, etc. From some search engine using PHP snippet. This script might usefully when you want to analyze your keyword and referer statistic. Last night I was searching in WP plugin for this script but I didn’t found one really good.

So let’s created it our self, First, You need this PHP class save it as searchkeys.class.php

<?php

class search_keywords
{
var $referer;

var $search_engine;

var $keys;

var $sep;

function search_keywords()
{
$this->referer = ”;
$this->sep = ”;

if ($_SERVER[‘HTTP_REFERER’] OR $_ENV[‘HTTP_REFERER’])
{
$this->referer = urldecode(($_SERVER[‘HTTP_REFERER’] ? $_SERVER[‘HTTP_REFERER’] : $_ENV[‘HTTP_REFERER’]));
$this->sep = (eregi(‘(\?q=|\?qt=|\?p=)’, $this->referer)) ? ‘\?’ : ‘\&’;
}
}

function get_keys()
{
if (!empty($this->referer))
{
if (eregi(‘www\.google’, $this->referer))
{
// Google
preg_match(“#{$this->sep}q=(.*?)\&#si”, $this->referer, $this->keys);
$this->search_engine = ‘Google’;
}
else if (eregi(‘(yahoo\.com|search\.yahoo)’, $this->referer))
{
// Yahoo
preg_match(“#{$this->sep}p=(.*?)\&#si”, $this->referer, $this->keys);
$this->search_engine = ‘Yahoo’;
}
else if (eregi(‘search\.msn’, $this->referer))
{

// MSN
preg_match(“#{$this->sep}q=(.*?)\&#si”, $this->referer, $this->keys);
$this->search_engine = ‘MSN’;
}
else if (eregi(‘www\.alltheweb’, $this->referer))
{
// AllTheWeb
preg_match(“#{$this->sep}q=(.*?)\&#si”, $this->referer, $this->keys);
$this->search_engine = ‘AllTheWeb’;
}
else if (eregi(‘(looksmart\.com|search\.looksmart)’, $this->referer))
{
// Looksmart
preg_match(“#{$this->sep}qt=(.*?)\&#si”, $this->referer, $this->keys);
$this->search_engine = ‘Looksmart’;
}
else if (eregi(‘(askjeeves\.com|ask\.com)’, $this->referer))
{
// AskJeeves
preg_match(“#{$this->sep}q=(.*?)\&#si”, $this->referer, $this->keys);
$this->search_engine = ‘AskJeeves’;
}
else
{
$this->keys = ‘Not available’;
$this->search_engine = ‘Unknown’;
}
return array(
$this->referer,
(!is_array($this->keys) ? $this->keys : $this->keys[1]),
$this->search_engine
);
}
return array();
}
}

?>

Now you have the classes, you can use this example PHP script to shown the result, save it as blabla.php $keys[3] will shown information about full referer URL so you can add or remove it.

<?php

require_once(‘./searchkeys.class.php’);

$keys =& new search_keywords();
$keys = $keys->get_keys();

if (count($keys))
{
echo “You’re directed to this page from <b>$keys[2]</b> search engine, within Keywords <b>’$keys[1]'</b>”;
}

?>

Now you can add this snippet to your page, The result is same like this screen shoot(blue box):

php-se-keyword-catch

Done, I hope someone would created WP plugins using this script and added some feature. That would be nice he he 🙂

Download the package here!

Similar Posts:

Related Search Terms:

    Digg Del.icio.us StumbleUpon Reddit Twitter RSS

If you're new here, you may want to subscribe to my RSS feed. You may copy or publish this article to your blog or other site as long you give credit link back to this site article. Thanks for visiting my blog!