Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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
}

This is an old question and the answer has changed since the original question was asked and answered. As of October 2011 Google is encrypting this referral information for anyone who is logged into a Google account: http://googleblog.blogspot.com/2011/10/making-search-more-secure.html

For users not logged into Google, the search keywords are still found in the referral URL and the answers above still apply. However, for authenticated Google users, there is no way to for a website to see their search keywords.

However, by creating dedicated landing pages it might still be possible to make an intelligent guess. (Visitors to the "Dignified charcoal sketches of Jabba the Hutt" page are probably...well, insane.)

This is an old question but I found out that google no more gives out the query term because it by default redirects every user to https which will not give you the "q"parameter. Unless someone manually enters the google url with http (http://google.com) and then searches, there is no way as of now to get the "q" parameter.

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.

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.

Same thing, but with some error handling

<?php
if (@$_SERVER['HTTP_REFERER']) {
    $referringPage = parse_url($_SERVER['HTTP_REFERER']);
    if (stristr($referringPage['host'], 'google.')) {
        parse_str( $referringPage['query'], $queryVars );
        $google = $queryVars['q'];
        $google = str_replace("+"," ",$google); }
    else { $google = false; }}
else { $google = false; }

if ($google) { echo "You searched for ".$google." at Google then came here!"; }
else { echo "You didn't come here from Google"; }
?>

Sorry, a little more
Adds support for Bing, Yahoo and Altavista

<?php
if (@$_SERVER['HTTP_REFERER']) {
    $referringPage = parse_url($_SERVER['HTTP_REFERER']);
    if (stristr($referringPage['host'], 'google.')
        || stristr($referringPage['host'], 'bing.')
        || stristr($referringPage['host'], 'yahoo.')) {
            parse_str( $referringPage['query'], $queryVars );
            if (stristr($referringPage['host'], 'google.')
                || stristr($referringPage['host'], 'bing.')) { $search = $queryVars['q']; }
                        else if (stristr($referringPage['host'], 'yahoo.')) { $search =     $queryVars['p']; }
                        else { $search = false; }
            if ($search) { $search = str_replace("+"," ",$search); }}
            else { $search = false; }}
else { $search = false; }
if ($search) { echo "You're in the right place for ".$search; }
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top