PHP Referrer: Displaying The Referring Page

How do you retrieve the referring page?

SHARE to your FRIENDS by CLICKING HERE:

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: http://cupidblogger.com/how-to-rock-your-single-world/

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: ๐Ÿ™‚

SHARE to your FRIENDS by CLICKING HERE:

7 thoughts on “PHP Referrer: Displaying The Referring Page”

  1. I have to admit James I skipped most of the technical stuff in that post. I hope someone can help you out with shortening the code and all, and it ain’t going to be me. ๐Ÿ˜‰

    As to the video, now those are some impressive dives. You would have thought though with all the extra time they had they could have come up with a triple or something ๐Ÿ˜€
    .-= Sireยดs latest blog ..Where Bloggers Meet, A Forum For Bloggers =-.

    1. Sire, after reading your posts this past couple of months at wassupblog.com, I realized that there’s much more to you than just being a blogger. Your painting talent for instance sort of took me by surprise, although I should have known this earlier. A blogger who belts out poetry is an artist with words. I should have connected the dots and found the painter in you too. ๐Ÿ™‚

      Whenever I find myself with my family in a pool or at the beach, the swimming is never 100% enjoyable if I can’t do some basic dives (and basic dive is the only dive I know). Thus I tend to suggest pools with diving boards whenever anyone thinks of us going out swimming. Why do I have this feeling that you Sire has some aptitude for diving too? For a multi-talented man such as you, I would not be surprised anymore if you do. ๐Ÿ™‚

  2. Hi James! I just forced myself into this in order to scratch an itch. ๐Ÿ™‚ I was really curious how they do it. And recently, I came to know about Google’s ‘smart pricing’ in adsense. They say you get smart-priced if your CTR is low and so you only earn a percentage per click of the normal rate. The above codes, turns out, could be used as a solution to the smart pricing thingy, so that one could display adsense ads only to visitors who came to one’s site through the search engines. These visitor types are supposed to be the ones who are most likely to click on the ads. CTR thus increases and everyone is happy. ๐Ÿ™‚
    .-= james_mdeoยดs latest blog ..HTML For Kids =-.

Comments are closed.