Liam Delahunty: Home Tips Web Contact
Recommended laptop
under £500
.

Think I deserve a present? See my Amazon Wish List

PHP Google Referrer Key Phrase

Sometimes it can be useful to know why a visitor arrived on a certain page. For instance, if you have a FAQ page on a variety of topics, you could provide a quick menu related to the terms that the user is interested in, before the remainder of the page. Alternatively, you could rewrite the page so that the terms are highlighted or indeed redirect the user to a more specific page altogether.

I wrote the following exert as part of a larger script that provided search results for pages that had a low match to the keyterm in the main content.

Certain pages on a website were getting referrals because of navigational links to other pages, so people where hitting the 'widget' page, when they had really wanted the 'thingy' page because the original widget page mentioned and linked to the 'thingy' page. With this as a plug-in on the page, we could help provide a more appropriate page and a better user experience for our visitors.


// 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);
}	


Share this!