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.
$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'";