PHP:Catching keyword from search engine

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:

26 thoughts on “PHP:Catching keyword from search engine”

  1. can you please elaborate about how to install this. I am getting an error message

    Parse error: syntax error, unexpected ‘/’ in /home/onefeeds/public_html/istanto/blabla.php on line 3
    I do own something similar for wordpress.

  2. Use your own server path, sorry I think blabla.php mean any name you want.. I will uploaded .rar package in the future…

  3. This tip may help:

    Since the above PHP code was posted via WordPress, the single and double quotes have been curled. This will NOT work in PHP, so you will have to go through and replace the curled quotation marks with regular “straight” ones.

    HTH

  4. yes Matt that’s actually the problem I didn’t paste it from WORDS or NOTEPAD when publish this article, I will uploaded it in .rar package soon.

  5. hey this is great work.
    but one problem. The function eregi is going to remove. check php.net documentation. They saying “Warning->>This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.”

  6. Good day!
    Fine script, thanx a lot.
    I came to your site while searching the keywords stat. service. You know, where one can see the stat. list of keywords from all SE, in the context of different spheres, for exsample – cinema or computers. Do you know such a service?

  7. Hey – thank you very much! I haven’t tried this script yet, but it looks like exactly what I need.

    This is a great tool for SEO and Conversion optimization people. Think about it, you want/need to know which keywords are producing the highest conversion rate (buying something, leaving contact info).

    You can obviously set this up with Analytics, but if you’re looking to develop a custom application or show the client which keywords people used to get to their site and fill out a contact form, this script can help do the trick

    Thanks again!!

  8. Hey,
    When I search http://www.google.com in yahoo and give the link as an input to the function, it returns that the search engine is Google.

    As a fact, no search engine will redirect the user to my page, when someone searches for google.com. But still, just wanted to tell you…

    Regards

  9. Hey, you said you wanted someone to use your code in a WordPress plugin. I just did that. I call it asearch on search widget. What it does it uses your stuff here to check for search keyword and if it finds any it presents links to other pages and posts on the site that matches the search too.

    While at it I ironed out some bugs in the search_keyword class. You’ll find a link to the source code of the widget in that blog post.

    Many thanks for sharing!

  10. Great article. I just want to add a comment and offer a suggestion.

    I used your code to log referrals to my web site but some keywords were truncated especially words and phrases that include ampersands like (at&t).

    My solution was not to use ‘urldecode’ in the search_keyword function.

    Insead of this:
    $this->referer = urldecode ( ( $_SERVER[‘HTTP_REFERER’] ? $_SERVER[‘HTTP_REFERER’] : $_ENV[‘HTTP_REFERER’] ) );

    use this
    $this->referer = ( $_SERVER[‘HTTP_REFERER’] ? $_SERVER[‘HTTP_REFERER’] : $_ENV[‘HTTP_REFERER’] );

    and then urldecode the keywords in the get_keys function.

    Insted of this
    // Google
    preg_match ( “/{$this->sep}q=(.*?)\&/si”, $this->referer, $this->keys );
    $this->search_engine = “Google”;

    Use this
    preg_match ( “/{$this->sep}q=(.*?)\&/si”, $this->referer, $matches );
    $this->keys = urldecode ( $matches[1] );
    $this->search_engine = “Google”;

    Anyway I want to thank you for saving me time on today’s project.

    Robert,

  11. Don’t forget to add Bing 🙂

    // Microsoft Bing
    preg_match ( “/{$this->sep}q=(.*?)\&/si”, $this->referer, $matches );
    $this->keys = urldecode ( $matches[1] );
    $this->search_engine = “Bing”;

  12. I only receive this on my page:

    get_keys(); if (count($keys)) { echo “You’re directed to this page from $keys[2] search engine, within Keywords ‘$keys[1]’
    “; } ?>

    (it is a .shtml and I set the httpd.conf to AddType application/x-httpd-php .shtml)

  13. Not so good …

    Too much regex calls. They are heavy functions retarding execution time. if i am the last of 50 search engines, it will have to execute 50 eregi before finding me. Remember that this script is called on almost every pages. In fact, it s called on the most importants pages, the ones we want to target. So we want it fast. Try this code, it makes the same :

    $url = parse_url($_SERVER[‘HTTP_REFERER’]);
    $possibles_params = array(‘q’,’qt’,’p’);//&q= &qt= &p= …
    parse_str($url[‘query’], $params);
    foreach($possibles_params as $val)
    {
    if($params[$val]) echo “referer : “.$url[‘host’].”keywords : “.$params[$val];
    }

    Note that it keeps the complete referer name server. Because it can be useful in SEO to know if it is refering from http://www.google.com, http://www.google.fr, http://www.google.co.uk, etc …

    Good luck !

  14. Google Keyword Search

    I am no longer positive where you’re getting your information, but great topic. I must spend a while finding out much more or understanding more. Thank you for wonderful information I used to be searching for this information for my mission.

  15. I drop a comment each time I especially enjoy a post on
    a site or I have something to valuable to contribute to the
    conversation. It’s a result of the fire communicated in the post I browsed. And after this post PHP:Catching keyword from search engine – Istanto Blog – Online Business, Short Reviews, Computers and Internet, Tips and Trick, Make Money Online. – Online Business, Short Reviews, Computers and Internet, Tips and Trick, Make Money Online.. I was actually moved enough to drop a thought 🙂 I actually do have a few questions for you if it’s allright.
    Is it just me or do a few of the comments come across like they are
    written by brain dead individuals? 😛 And, if you are writing on other
    online social sites, I would like to keep up with you.
    Would you list all of all your communal sites like your twitter
    feed, Facebook page or linkedin profile?

  16. It’s a pity you don’t have a donate button! I’d certainly donate to this brilliant blog!
    I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account.
    I look forward to fresh updates and will share this website with my Facebook group.
    Chat soon!

  17. I’m not that much of a online reader to be honest but your sites really
    nice, keep it up! I’ll go ahead and bookmark your site to come back later on. Many thanks

  18. Hi there, You have done a fantastic job. I will definitely digg it and personally recommend to my friends.
    I’m confident they’ll be benefited from this site.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.