Extract the search term from google referer

$referer = strtolower($_SERVER['HTTP_REFERER']);
 
// Test if user comes from google
if (strpos($referer, 'google')) {
	// Delete all before &q=
    $tmp = substr($referer, strpos($referer, 'q='));		
 
    // Remove q=
	$tmp = substr($tmp, 2);
 
	// Remove everything after the next &
	if (strpos($tmp, '&')) {
		$tmp = substr($tmp, 0,strpos($tmp, '&'));
	}	
	// we have the results.
	$searchTerm = urldecode($tmp);
}

Download this snippet

Takes the referer and extracts the search term, if user comes from google.

Twitter Twitter

5 Comments to “ Extract the search term from google referer”

  1. Anonymous  on Sep 25, 2009

    ZnyetY <a href="http://fiwyshsjgiof.com/">fiwyshsjgiof</a>, [url=http://girhneklpavq.com/]girhneklpavq[/url], [link=http://hykhzxnbbamr.com/]hykhzxnbbamr[/link], http://ubctpwheottt.com/

  2. Anonymous  on Oct 12, 2009

    Very handy. Never knew about the strpos and substr functions, I have a few ideas for how to use them in a couple other scripts I want to put together, but never knew which functions or how, thanks!!

  3. Anonymous  on Nov 04, 2009

    T3hptm <a href="http://pauormsyttib.com/">pauormsyttib</a>, [url=http://vscmuwlupphj.com/]vscmuwlupphj[/url], [link=http://efwegxhvfuhr.com/]efwegxhvfuhr[/link], http://qnijjxnzlxpu.com/

  4. Anonymous  on Dec 28, 2009

    7cMXpK <a href="http://cxycnbfhllkm.com/">cxycnbfhllkm</a>, [url=http://ojzaewbumelg.com/]ojzaewbumelg[/url], [link=http://nsixmmkrvedk.com/]nsixmmkrvedk[/link], http://ujhsshrprihl.com/

  5. Anonymous  on Jan 13, 2010

    zjvobm <a href="http://hquwcuyxyrnc.com/">hquwcuyxyrnc</a>, [url=http://wstslphbeixb.com/]wstslphbeixb[/url], [link=http://wmfxpmmtwvqb.com/]wmfxpmmtwvqb[/link], http://cwntrnhocuqa.com/

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>