5ubliminal@twitter

How to get Query for Refering Search Engine from Referer in PHP : 5ubliminal's TellinYa

<a href="http://www.tellinya.com/art2/34/">How to get Query for Refering Search Engine from Referer in PHP : 5ubliminal's TellinYa</a>
Must Reads: Web Scraping | Link Farming | Code Snippets | SEO Freeware » I'm back to work … sorting my shit now :)
Reveal More!

I made a lot of changes recently including a fix. Now it will also work with DogPile and fixed some more bugs on it. If you know of Referers it does not nail down please send them over in the comment form and I'll add them to the site!

What is the Search Engine Refering Query?

The search engine refering query is the text users searched for and found your site. Look at the image below for example :

You can see how I searched for search engine refering query in Google and found this page.

Decoding Search Engine Refering Query

You will notice many sites show you the query you used in google or other search engines to find them and sometimes mark the search terms in their content with colors.

By knowing what query was used in search engines to find you you can do several other things. You can guide visitors to further content based on their searches, you can record queries and have a simple status of your own for the website.

Compatible search engines

This function translates searches from Google, Yahoo, MSN, Ask, AOL, Digg and others that have the same query format.

SeReferer Function PHP Script Code

The function will deliver an array containing several fields as explained below.

<?
// ----------------------------------------------------------------------------
/* $ref is optional, if not set will use current! */
function seReferer($ref false){
    
$SeReferer = (is_string($ref) ? $ref $_SERVER['HTTP_REFERER']);
    if( 
//Check against Google, Yahoo, MSN, Ask and others
        
preg_match(
        
"/[&\?](q|p|w|searchfor|as_q|as_epq|s|query)=([^&]+)/i",
        
$SeReferer,$pcs)
    ){
        if(
preg_match("/https?:\/\/([^\/]+)\//i",$SeReferer,$SeDomain)){
            
$SeDomain    trim(strtolower($SeDomain[1]));
            
$SeQuery    $pcs[2];
            if(
preg_match("/[&\?](start|b|first|stq)=([0-9]*)/i",$SeReferer,$pcs)){
                
$SePos    = (int)trim($pcs[2]);
            }
        }
    }
    if(!isset(
$SeQuery)){
        if( 
//Check against DogPile
            
preg_match(
            
"/\/search\/web\/([^\/]+)\//i",
            
$SeReferer,$pcs)
        ){
            if(
preg_match("/https?:\/\/([^\/]+)\//i",$SeReferer,$SeDomain)){
                
$SeDomain    trim(strtolower($SeDomain[1]));
                
$SeQuery    $pcs[1];
            }
        }
    }
    
// We Do Not have a query
    
if(!isset($SeQuery)){ return false; }
    
$OldQ=$SeQuery;
    
$SeQuery=urldecode($SeQuery);
    
// The Multiple URLDecode Trick to fix DogPile %XXXX Encodes
    
while($SeQuery != $OldQ){
        
$OldQ=$SeQuery$SeQuery=urldecode($SeQuery);
    }
    
//-- We have a query
    
return array(
        
"Se"=>$SeDomain,
        
"Query"=>$SeQuery,
        
"Pos"=>(int)$SePos,
        
"Referer"=>$SeReferer
    
);
}
// ----------------------------------------------------------------------------
?>

How the function works

The function compares the referer to a list of known search query strings: //Google.com referer URL sample:
http://www.google.com/search?hl=en&q=referer+search+engine+query
//Yahoo.com referer URL sample:
http://search.yahoo.com/search?p=refering+search+engine+query
//The Known variable names used by search engines:
q|p|w|searchfor|as_q|as_epq|query
As you see the user's query is container in the q parameter of the referer query string. Now look at the regular expression used :

Try this to see the results after you visit your pages by doing a site:your-site.com search in Google. Use this PHP code: $se=seReferer(); print_r($se);

14 Comments Posted By Readers :

Add your comment
#1 Mark from Great Britain
Posted on Tuesday, 14 August, 2007
Hi, I tried your script but I got the following error.

Warning: Missing argument 1 for sereferer() in /home/markyo/public_html/fix.php on line 29

On line 29 it reads

function seReferer($ref){

Any ideas?

Regards, M
#2 5ubliminal web
Posted on Tuesday, 21 August, 2007
Hi Mark,

When I coded this function I added the option to pass a referer string and have it parsed. This way you can use it on other referer strings and process them too, not only the referer string passed when the current page was loaded. But the parameter is optional. If it is not set the current referer string is used by default.

So, as you mentioned, this was not an error but a warning which any coder should disable by using the error_reporting PHP function. If you did not pass any parameter the warning tells you so. But this is not fatal just a notice from PHP trying to somehow aknowledge the fact that this is what you really want from your code.

I altered the function so it will not generate error but do look into the function I mentioned earlier and use it. Take control over the errors and do not allow PHP to write any warning or notice on your pages.

For any further information do not hesitate to reply.
And stay tuned as I will write an article on errors and warnings quite soon.
#3 markowe from Czechoslovakia web
Posted on Monday, 03 September, 2007
Cool bit of code, I've been looking for something like this. Strikes me that this has a great deal of potential for dynamically generating pages of content that the user was searching for - indeed, that probably IS what it's used for, I'm just very slow on the uptake!
#4 markowe from Czechoslovakia web
Posted on Tuesday, 04 September, 2007
You think that would be classed as black-hat tactics... Hmmm, I suppose so, if you generated crappy generic code for any search term that came along... I guess this would still have to be just a way of targeting a good base of original content.
#5 5ubliminal web
Posted on Tuesday, 04 September, 2007
I suggest only changing few things here and there (ads mostly).
Do what you wish but think of passing human review.

I would not alter content but the ads and some affiliate links or images can be messed around with.

Caution!
#6 Deian Motov from Bulgaria web
Posted on Tuesday, 27 November, 2007
Thank you for this code, it really helped me out, wish you all best.
#7 5ubliminal web
Posted on Wednesday, 28 November, 2007
You're welcome and thanks.
#8 Pony from South Africa web
Posted on Friday, 11 January, 2008
Nice bit of code thanks. I was just going to check if the request object had anything in it.
#9 Sharry from Great Britain web
Posted on Monday, 14 April, 2008
Hi, thanks for your great code. If I wanted to add more engines could I add more possibilites in the preg_match?:
preg_match("/[&?](q|p|w|searchfor|as_q|as_epq|s|query|arg|search|qt|keyword|keywords|encquery|qry)=([^&]+)/i", $SeReferer,$pcs)

// used by chubba
'arg='
// used by dmoz
'search='
// used by looksmart
'qt='
// used by scrub the web
keyword='
// used by overture, hogsearch
'keywords='
// used by mamma, lycos, kanoodle, snap, whatuseek
query='
// don't allow encrypted key words by aol
'encquery='
// used by ixquick
'&query='
// used by aol
qry='
#10 5ubliminal web
Posted on Monday, 14 April, 2008
Ur welcome.
Add them there but make sure you test it on some referrers.
It should work. Add keyword_identitfier and | like you see the ones before.
#11 markowe from Czechoslovakia
Posted on Thursday, 17 April, 2008
Hi, I came back to this thread after several months and was amused to see that I am from Czechoslovakia, all because I happened to be at a conference there at the time I wrote my comments :) (although Czechoslovakia as such hasn't existed for almost 20 years!)

Anyway, your little code is really handy - I wanted to ask if you would mind me using it in a project I am working on. The project is a free GNU/GPL plugin for WordPress, where I am thinking of adding the possibility of highlighting search keywords to the content, which your code would be perfect for (though that's not the main point of the plugin) and I would give you credit of course. It's just, why go to the hassle of writing it myself when you've done such a great job? :) Plus those regular expressions really stress me out!
#12 5ubliminal web
Posted on Thursday, 17 April, 2008
You can use it … no problem. Especially if credited :)
PS: It's the IP2Country Country names. I'll have to check them a bit because this is indeed interesting.
#13 Sharry from Great Britain web
Posted on Friday, 18 April, 2008
Hi, can you explain what the '$ref' does please ?
Thanks
#14 5ubliminal web
Posted on Friday, 18 April, 2008
:) $ref = $_SERVER['HTTP_REFERER]; The referer string.
Post Feedback 
Name *
Mail *
URL
« Anti-Spam
» URL will only go live after a review. Comments are moderated. «
5ubliminal's TellinYa.com SEM & SEO Blog © 2007 - All rights reserved unless mentioned otherwise .
Rendered On : [Tuesday, 07 October, 2008 - 12:47:19 GMT]   No Ajax / Flash Used Here
" How to get Query for Refering Search Engine from Referer in PHP : 5ubliminal's TellinYa "