In this quick tip, I’ll show you how to display custom messages to your visitors depending on where they click from onto your website. I saw this technique and at first thought, how is this being done?! But I’d forgot the value’s PHP can store in $_SERVER

Below is the full code

If you want to quickly grab the code and get this up and running just copy and paste this code below into your site. This will detect people coming from Google or Twitter and display a message accordingly. If you’d like to know how it all works, read on after the snippet.

<?php
 
	// if a http refferer is set, continue
	// this prevents any kind of error message if it's not set
 
	if(isset($_SERVER['HTTP_REFERER'])) {
 
		// set the http refferer into the value $ref
 
		$ref = $_SERVER['HTTP_REFERER'];
 
		// if the $ref value contains 'twitter', continue
 
		if(stristr($ref, 'twitter')) {
 
			// echo the statement
 
			echo 'Welcome Twitter user';
 
		} elseif (stristr($ref, 'google')) {
 
			// echo the statement
 
			echo 'Welcome Google user';
 
		}
	}
 
?>

How all this fan-dango works

Here we open an if statement to check if the server has the HTTP referrer set, sometimes servers don’t set this value (though the majority should do) and will give you a nasty error message if they don’t, so this bit of code will prevent that from happening.

<?php
 
	if(isset($_SERVER['HTTP_REFERER'])) {

Now if the referrer is set we then store it in value of $ref for later use.

 
		$ref = $_SERVER['HTTP_REFERER'];

Now for the important bit. I’ve now opened up another if statement that uses the function stristr to search through the HTTP referrer (remember, our HTTP referrer will be set in $ref) for the word ‘twitter’. If our statement is true, it will then say ‘Welcome Twitter user’. Though this is the most simple of uses, you could use it to display a follow message or subscribe details, whatever your noggin can think of.

 
		if(stristr($ref, 'twitter')) {
 
			// echo the statement
 
			echo 'Welcome Twitter user';
 
		}

That’s about it for this one, I’m thinking of maybe turning this into a plugin for WordPress using my Social Media Icons in the process, yes… no..?

Author: Nouveller

My name is Benjamin Reid, I live in the UK and keep the magic locked into this site.

RSS Icon

Subscribe to the RSS feed some design goodness, get in on the upcoming software give-aways!

Digg Icon Share Quick Tip #4 - Display custom messages depending on where your visitors are coming from to Twitter Share Quick Tip #4 - Display custom messages depending on where your visitors are coming from to Delicious Share Quick Tip #4 - Display custom messages depending on where your visitors are coming from to Facebook

Related post(s)

Comments left by other wizards

One Response, yes you are first! | Make a comment

Leave your words of wisdom

Leave a Reply

This is a Gravatar

Your name