Capturing search queries from Google and Yahoo using PHP
In the world of online lead generation, it’s really important to understand where your lead sources come from, particularly if they originate from a search engine query. In the code snippet below, I show you how to use PHP to discretely unpack an incoming request and figure out if it is Google or Yahoo, and if so, how to isolate the original search engine query the user entered.
$referer_string = $_SERVER['HTTP_REFERER'];
$uri = explode('?',$referer_string);
$args = explode('&',$uri[1]);
foreach ($args as $v){
$items = array();
$items = explode('=',$v);
if ($items[0] == 'q' or $items[0] == 'p'){
#we have some kind of query string from google or yahoo!
#could also use regular variable or cookie!
$_SESSION['search_string'] = $items[1];
}
}

