views:

899

answers:

4

This may be a stupid question, but is it possible to capture what a user typed into a Google search box, so that this can then be used to generate a dynamic page on the landing page on my Web site?

For example, let's say someone searches Google for "hot dog", and my site comes up as one of the search result links. If the user clicks the link that directs them to my Web site, is it possible for me to somehow know or capture the "hot dog" text from the Google search box, so that I can call a script that searches my local database for content related to hot dogs, and then display that? It seems totally impossible to me, but I don't really know. Thanks.

+8  A: 

Yes, it is possible. See HTTP header Referer. The Referer header will contain URL of Google search result page.

When user clicks a link on a Google search result page, the browser will make a request to your site with this kind of HTTP header:

Referer: http://www.google.fi/search?hl=en&q=http+header+referer&btnG=Google-search&meta=&aq=f&oq=

Just parse URL from request header, the search term used by user will be in q -parameter. Search term used in above example is "http header referer".

Same kind of approach usually works also for other search engines, they just have different kind of URL in Referer header.

This answer shows how to implement this in PHP.


Referer header is only available with HTTP 1.1, but that covers just about any somewhat modern browser. Browser may also forge Referer header or the header might be missing altogether, so do not make too serious desicions based on Referer header.

Juha Syrjälä
Excepting that, to have your page turn up in search results it already needs to have relevant content
kdgregory
A: 

Yes, it comes in the url:

http://www.google.com/search?hl=es&q=hot+dog&lr=&aq=f&oq=

here is an example:

Google sends many visitors to your site, if you want to get the keywords they used to come to your site, maybe to impress them by displaying it back on the page, or just to store the keyword in a database, here's the PHP code I use :

// take the referer
$thereferer = strtolower($_SERVER['HTTP_REFERER']);
// see if it comes from google
if (strpos($thereferer,"google")) {
    // delete all before q=
    $a = substr($thereferer, strpos($thereferer,"q="));  
    // delete q=
    $a = substr($a,2);
    // delete all FROM the next & onwards
    if (strpos($a,"&")) {
     $a = substr($a, 0,strpos($a,"&"));
    } 
    // we have the results.
    $mygooglekeyword = urldecode($a);
}

and we can use <?= $mygooglekeywords ?> when we want to output the
keywords.
tekBlues
Wow, thanks everyone. I am one humbled newbie. I posted my question FOUR MINUTES ago and got back three helpful replies. Stack Overflow rules!
johnnyb10
There are much better ways to do it than this. PHP has a built in function to parse a url. parse_url, parse_str makes this 3 lines.
Paolo Bergantino
A: 

You can grab the referring URL and grab the search term from the query string. The search will be in the query as "q=searchTerm" where searchTerm is the text you want.

Andrew Van Slaars
+9  A: 

I'd do it like this

$referringPage = parse_url( $_SERVER['HTTP_REFERER'] );
if ( stristr( $referringPage['host'], 'google.' ) )
{
  parse_str( $referringPage['query'], $queryVars );
  echo $queryVars['q']; // This is the search term used
}
Peter Bailey
+1 much cleaner way to do it.
Paolo Bergantino
+1 for making code compatible with international google domain names (google.fi, google.de etc) and with google image search.
Juha Syrjälä
thanks! I figure there's still a potential here for a false positive - somebody with google.domain.com - but this was only an example. You could tighten it up by also checking the value of $referringPage['path'] and, after that, a simple isset() check on $queryVars['q']
Peter Bailey
Instead of stristr() use preg_match("/google\.[a-z]{2,4}$/i", $referringPage['host']) to prevent the google.domain.com case mentioned above
Swish
That's a good attempt Swish, and I actually though of that, but then I remembered this: http://www.google.co.uk/ ;)
Peter Bailey
@Peter Bailey, I don't think that false positives matter too much. Browser can fake this header anyways.
Juha Syrjälä