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):
Done, I hope someone would created WP plugins using this script and added some feature. That would be nice he he
SIMILAR POST :
- SEO: URL Rewrite for 4images
- How To Stop WP-CRON.php From Using To Much CPU Usage
- HOW TO:Get flooded traffic daily and raise your pagerank.
- PHP: Creating Custom Title and Meta (Simple Way)
Incoming search terms:
- php detectar referer adwords
- google catch search words
- get referer search key google php
- php get search engine and keywords
- detect search engine referral
- catching keywords search engines
- parsing seo from referrer
- capture search engine keywords in php
- capture keywords on my site php
- php suchbegriffe aus referer ermitteln
- php parser google adwords keyword
- php parse referring keywords
- php parse referrer search engine
- php google refer keyword
- php google referrer key phrase
- php keyword referral
- php keyword parser
- php google normal referer adwords
- php get search engine keyword preg_match
- php get search engine keyword
- php code to find key keywords in google
- php détection keyword
- php detect adwords refer
- php detect keywords
- php detect search engine and keyword
- php detectar keyword referrer
- php extracting keywords from referrer
- php find keywords in string
- php get google keyword idea
- php get keywords from search engine referer url
- php code for keyword position in google
- php recognise adwords
- referring keyword display script
- retrieve search keywords in asp
- save search keywords php
- saving the google referrer keyword
- script referrer Search list keywords
- search englines referal keywords using PHP
- search keywords in php
- vba parse google referrer strings
- website php capture adwords keywords
- what is my referer and google keywords
- referral keywords from google script save
- referal keyword php
- recognising adwords in referer
- php recognise referral from google
- php recognize adwords
- php referrer google keywords class
- php save keyword function refer from google search
- php saving search keywords
- php script save google keywords
- php search referrer key
- PHP: get keywords from search engine referer url
- preg_match google url_referer
- preg_match referrer from google adwords
- wordpress plugin keyword referrer
- $get php suchbegriffe referer
- detect referer cpc organic
- detect search engine and query from referrer
- detect search engine keyowrds php
- detect search engine keyword in php
- detect search engine referrer php
- detectar referer adwords organic cpc
- detecting adwords referers
- display last google referrer keywords wordpress
- extract keyword referrer
- extract keyword referrer url
- detect google referrer
- detect google keywords
- abn amro bursa efek jakarta alamat
- adwords extract keywords script from referrer
- adwords searchterm php
- can save keyword in php
- catch google keyword php
- catching keywords from search engines
- code php search again for domain
- detect google ad referer
- detect google adwords referers
- detect google analytics preg_match
- extract referer keywords php google 2011
- get bing search terms referer php
- get keyword by referer
- lokasi bank of america jakarta
- parse google search referral
- parse html php analyzing keywords
- parse keyword in php
- parsing google referrer string to get search key
- php adwords google keyword various word
- php capture search engine keywords
- php check refer is from adwords
- php class google search referrer keywords redirect
- php class search_keywords not working keywords empty
- keywords search engine preg_match_all php
- keywords positions php script
- get refferer keywords wordpress
- get search engine referral keyword script
- getsearchenginekeywords
- google adwords fetch referrer keyword
- google реферер keyword
- google searches related to preg_match_all(
- googleadwords php class
- how get search engine position from referer
- how to parse google referer php
- keyword referer script
- php class to parse search engine referral strings
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!
22 Responses to “PHP:Catching keyword from search engine”
Trackbacks
- PHP:Catching keyword from search engine » Istanto Blogs | Search Engine Secrets
- First Position At Google By Make Money Blog
- A search on search widget « Bagonca
- web site development







Did you know?
Tag cloud
Blogs Statistic
Subscribe my feed

June 6th, 2009 at 8:37 PM
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.
June 7th, 2009 at 6:06 AM
Use your own server path, sorry I think blabla.php mean any name you want.. I will uploaded .rar package in the future…
June 23rd, 2009 at 9:12 PM
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
June 24th, 2009 at 1:28 PM
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.
June 25th, 2009 at 4:27 PM
I just updated download link in the bottom article.
September 3rd, 2009 at 10:19 AM
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.”
November 15th, 2009 at 8:48 AM
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?
November 16th, 2009 at 1:23 PM
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!!
December 3rd, 2009 at 11:09 PM
Excellent job dude. I was searching for this last day. and in the morning found your great tutorial
December 26th, 2009 at 10:42 AM
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
January 14th, 2010 at 3:54 PM
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!
February 3rd, 2010 at 2:11 AM
This not working on query (ex. Google) like this: http://www.google.com/search?hl=en&q=example+keywords
I see this class search in query for \? if engine is Google (firtst parameter).
February 25th, 2010 at 10:37 PM
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,
February 25th, 2010 at 10:41 PM
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”;
March 8th, 2010 at 4:59 PM
You keywords will be empty when try to get from
http://www.google.com/search?hl=en&q=example+keywords
this query has q=example+keywords but not end with &
March 21st, 2010 at 4:53 PM
if a great script. thank you…
April 3rd, 2010 at 7:21 AM
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)
June 10th, 2010 at 10:04 PM
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 !