Top

Extract Search Query from HTTP Referer


PHP Code
+ 3 likes
Please Register to submit score.
Bookmark and Share
Average Score  8.0 (of 2 scores)
Date Added  Nov 19, 2008
Last Updated  Nov 19, 2008
Tags  google  http  query  referer  search 

Introduction

This will extract the search query from a Google search. It doesn't specifically look for Google in the referer so it may capture more than just Google searches. You can use this information to re-perform the search on your own site or just keep it for your own records.

Grab the Code

$referer = $_SERVER[HTTP_REFERER];
 
if($referer)
{
    preg_match("/[\&\?]q=([^&]*)/", $referer, $matches);
 
    if($matches[1])
    {
        $search_query = rawurldecode($matches[1]);
        $search_query = str_replace("+", " ", $search_query);
    }
}
 
print "You searched Google for '$search_query'";

Comments

  (4)  RSS
lostdeviant
Comments: 9
 
PHP Snippet:  Extract Search Query from HTTP Referer
Posted on Nov 19, 2008 2:36 pm
Nice snippet. I'd love a version for search engines that don't use q=
Hawkee
Comments: 1,039
 
PHP Snippet:  Extract Search Query from HTTP Referer
Posted on Nov 19, 2008 2:57 pm
You can just change the preg_match to work with any other engine. Just change the q to the name of the variable.
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  Extract Search Query from HTTP Referer
Posted on Nov 19, 2008 9:48 pm
Nice, Very Nice :)
vaseline28
Comments: 162
 
PHP Snippet:  Extract Search Query from HTTP Referer
Posted on Nov 20, 2008 3:10 pm
Really like this :)
+ Like

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom