PHP Referrer: Displaying The Referring Page

The PHP Referrer function is what coders use to retrieve the referring page of a particular web page. I have often wondered at some sites that greet me as soon as I land on their page with: “You were searching for ‘http referrer’. You may also find these other related articles useful:”. How did they do it?

As my site is a wordpress one, I first tried searching for wordpress plugins. I found some and was about to try ‘search on search‘ when I remembered that my blog had already amassed a chaos of plugins and recently found that some are conflicting with other plugins. So, I decided to do a little research outside the WP fence and thought that if I find some suitable PHP codes, I could try using it on my posts. (Inserting PHP codes, of course, is possible now thankfully because of the Exec-PHP plugin I installed on my blog).

With the help of the Online PHP manual, google, and very generous PHP webmasters, I came up with a crude set of codes that works (as far as my tests are concerned). I’m pretty sure that there are shorter and elegant ways to get the same results. I’m no PHP master, so if you have that shorter and elegant set of codes that does the same thing as the ones I concocted below, please help me out.

Using HTTP Referrer Function

$referersite = strtolower($_SERVER['HTTP_REFERER']);
if ((strpos($referersite,"google")) || (strpos($referersite,"bing"))
   || (strpos($referersite,"yahoo")) || (strpos($referersite,
   "digg")) || (strpos($referersite,"altavista"))) {
    if ((strpos($referersite,"google")) || (strpos($referersite,
        "bing")) || (strpos($referersite,"altavista"))) {
	if(strpos($referersite,"google")) {
	    	echo "You came from GOOGLE <br />";	
    	} elseif (strpos($referersite,"bing")) {
		echo "You came from BING <br />";
    	} elseif (strpos($referersite,"altavista")) {
		echo "You came from ALTAVISTA <br />";
    	$kp = substr($referersite, strpos($referersite,"q="));		
    } elseif (strpos($referersite,"yahoo")) {
	echo "You came from YAHOO <br />";
    	$kp = substr($referersite, strpos($referersite,"p="));	
    } elseif (strpos($referersite, "digg")) {
	echo "You cam from DIGG <br />";
    	$kp = substr($referersite, strpos($referersite,"s="));
   $kp = substr($kp,2);
   if (strpos($kp,"&")) {
  	$kp = substr($kp, 0,strpos($kp,"&"));
   $mykeyphrase = urldecode($kp);
   echo "searching for: ".$mykeyphrase; 
} else { 
   if (!empty($string)) {
	echo "You came here from: ".$referersite;

The code outputs the name of the search engine and the key phrase the visitor used to find the current page (if the visitor searched using google, yahoo, bing, altavista, or digg). If the visitor came to the page after clicking a link from some other site (not search engine), it’ll output the referring site’s url.

To test if the code worked, I installed it at the bottom of my ‘Great TinyURL Alternative’ post. And then I tried searching in the search engines. Since the Great Tinyurl Alternative post is not that ‘linked to’, I sometimes had to use long phrases to find it in the search engines.

search engine search phrase
bing tinyurl alternative hopefully still within the first page 🙂
google tinyurl alternative within the lower half of the serp, if not within the 2nd page
yahoo “I have searched for some time for a tinyurl alternative” In qoutes 🙂
altavista “I have searched for some time for a tinyurl alternative” In qoutes 🙂
digg tinyurl alternative probably the 3rd entry

So, if you clicked on the post’s page from the search engines serp shown above, it’ll bring you to the post and at the bottom of the post, you’ll see enclosed in a pinkish box:

You came from BING
searching for: tinyurl alternative

If you clicked my post’s links from another site (not a search engine), say, from the commentluv’s link from this post at kimwoodbridge or this post from Jamie’s, you’ll find at bottom of the post

You came here from:

As you may have noticed, I considered only google, bing, yahoo, altavista (and digg) among the search engines. There must be a better code that could wrap all these into a shorter version and cover all other search engines too. Like I said, I’m not that deep into PHP. Besides, I’ve only done this little research to satisfy my curiosity, although I’m entertaining an idea where I could use this for seo purposes.

Now, on to the lighter side: 🙂