Quick Tip #4 – Display custom messages depending on where your visitors are coming from

Posted on

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.


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.


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..?

3 comments

  • Liam Goodacre wrote on

    ooh nice tip :)
    thanks,

    and deff build as a WP plugin!

    -Liam Goodacre

  • Pingback: facebook fan page design

  • Demarcus Balm wrote on

    Howdy very nice web site!! Man .. Beautiful .. Superb .. I’ll bookmark your web site and take the feeds additionally…I am glad to search out so many helpful info right here within the post, we’d like develop extra techniques in this regard, thanks for sharing. . . . . . Exotic Escorts, 235a South Lambeth Road, London, SW8 1XR, 020 3011 2463

Add your comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>