Is it possible to capture search term from Google search?

johnnyb10 picture johnnyb10 · Jun 2, 2009 · Viewed 14.6k times · Source

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.

Answer

Peter Bailey picture Peter Bailey · Jun 2, 2009

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
}