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 Posts:
- SEO: URL Rewrite for 4images
- HOW TO:Get flooded traffic daily and raise your pagerank.
- Build And Submit Sitemap: Is It Really Necessary
- How To Stop WP-CRON.php From Using To Much CPU Usage
RELATED SEARCH TERMS:
- php detectar referer adwords
- php detectar referer adwords
- google catch search words
- google catch search words
- get referer search key google php
- get referer search key google php
- detect search engine referral
- detect search engine referral
- capture search engine keywords in php
- capture search engine keywords in php
- save search keywords php
- PHP: get keywords from search engine referer url
- detect search engine keyowrds php
- google adwords fetch referrer keyword
- php recognise adwords
- search keywords in php
- php google referrer key phrase
- getsearchenginekeywords
- how get search engine position from referer
- php capture search engine keywords
- detect search engine referrer php
- vba parse google referrer strings
- php suchbegriffe aus referer ermitteln
- php dÃtection keyword
- detecting adwords referers
- can save keyword in php
- extract referer keywords php google 2011
- php parser google adwords keyword
- Bank Central Asia – Sector: Finance mail
- php capture search engine keywords
- retrieve search keywords in asp
- php find keywords in string
- php google referrer key phrase
- search keywords in php
- google adwords fetch referrer keyword
- save search keywords php
- detect search engine keyowrds php
- php recognise adwords
- getsearchenginekeywords
- how get search engine position from referer
- php find keywords in string
- PHP: get keywords from search engine referer url
- vba parse google referrer strings
- php suchbegriffe aus referer ermitteln
- php dÃtection keyword
- detecting adwords referers
- can save keyword in php
- extract referer keywords php google 2011
- php parser google adwords keyword
- detect search engine referrer php
- retrieve search keywords in asp
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.
Use your own server path, sorry I think blabla.php mean any name you want.. I will uploaded .rar package in the future…
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
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.
I just updated download link in the bottom article.
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.”
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?
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!!
Excellent job dude. I was searching for this last day. and in the morning found your great tutorial
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
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!
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).
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,
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”;
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 &
if a great script. thank you…
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)
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 !
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.
This post provides clear idea designed for the new people
of blogging, that really how to do running
a blog.
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?
Еxсellent blog Amélie
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!
Excellent post. I’m going through a few of these issues as well..
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
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.