Display Greeting Message Depending on Social Media Referer – PHP

AD

By learning this tutorial you can display a greeting message to your visitor depending on the social media site (such as twitter, facebook etc.) they came from.

For instance if your visitor came to your site via Twitter, this piece of code will show them a customized greeting message with anything, and if you want; you want to add “follow us on Twitter” etc. kind of links as well. Same can be done for any social media site like Digg, StumbleUpon, Reddit and so on.

This code can be used in WordPress or any type of website developed in php.

First Step – Grab The Referrer Site Domain

For this we will use a simple php server function : $_SERVER[‘HTTP_REFERER’]; and store it in a variable. Then we will make that variable check the referrer site and display messages accordingly.

Code:

//Storing Referer info in a variable

$my_referer = $_SERVER['HTTP_REFERER'];
//echo $my_referer;
// We will be using If, Else Condition to check and display messages

if ($my_referer == 'http://twitter.com/') {    //if referer is twitter.com
echo "<div id='twitter'><p><b>Hello Twipple!</b> Thank you for visiting, you might enjoy following us on <br /><a href='http://www.twitter.com/Designzzz'><img src='http://www.designzzz.com/twitter-icon.png' alt='twitter' align='top' width='20px'> Twitter</a></p></div>";
}

else  {    //if referer is any other site
echo "<div id='default'><p><b>Hello Visitor!</b> Thank you for  visiting, you might enjoy Grabbing our Free<br /><a  href='http://feeds.feedburner.com/Designzzz'> <img src='http://www.designzzz.com/rss-icon.png' alt='RSS' align='top' width='20px'> RSS Feed</a></p></div>";
}

The CSS

p{color:#fff;font-size:18px;text-align:center;}
a {color:#333;text-decoration:none;font-weight:bold;}
a img{border:none;}
#twitter {width:50%;margin:10px auto;background:#C0DEED;padding:6px;border:1px solid #218FCE;}
#default {width:50%;margin:10px auto;background:#FBB363;padding:6px;color:#fff;border:1px solid #FF7200;}

Previews

For Twitter referrer preview go to this Link click on the link in tweet and see the message.

For direct/ default preview we will run the same link directly Link.

For WordPress

You can place this code in any file where you want it to display, for example for sidebar you can put it in sidebar.php. In case of post pages above title (recommended) You can place this code above the title code in Single.php.

Note:

If you will just open twitter.com or any other site and paste your URL there to see if it works it will always show you default message and not the message which should be displayed against that site. Referral works only if the link is clicked from that certain site.

create free website wix below post

Further Related Readings

Copyrights © 2016 - Designzzz. All Rights Reserved