How To Add a Custom Welcome Based on Referrer


I recently added a little snippet of PHP to Video Rambler to welcome people that were coming from StumbleUpon with a custom welcome. The website recently got Stumbled again and one of the people that gave the site a thumbs up stated:

I only gave it thumbs up bc I’m wondering how they put that stumbleupon welcome message at the top…only shows if you stumble it.

Here is how I did it. I check the $_SERVER[‘HTTP_REFERER’] to see if it is one of the referrers I want to give a special welcome to. If it is then I simply echo some css to add some padding to the top of the screen and print out the custom welcome. This might not be fool proof, since some browsers do not pass the referrer, or people could fake the referrer, but it works quite well for StumbleUpon. You could make this more complicated and display a different message for more than just Stumblers, but those were the only people I was interested in at this time. I have been using it for a month and considering how well the website has been doing with StumbleUpon I think I will keep using it for a little while.

<?php $allowed = Array('www.stumbleupon.com','stumbleupon.com'); $camefrom = array_change_key_case(parse_url($_SERVER['HTTP_REFERER'])); if (in_array($camefrom['host'],$allowed)) { echo '<style type="text/css">body { padding-top: 25px; } </style>'; echo '<div style="position: absolute; top: 0; left: 0px; width: 100%; padding: 3px 15px 3px 15px; background-color:#ffffe1;">Welcome Stumbler! Hope you enjoy the site and it would be great if you would give a nice <a href="javascript:document.location.href = \'http://www.stumbleupon.com/submit?url=\'+ document.URL +\'&title = \'+document.title.replace(/%20/g,\'+\');">Thumbs Up!</a> Enjoy!</div>'; } ?>

Categories: web-programming  
Comments