How to Display Different Content to Visitors from Different Sites

Every now and again you might want your website to display different information to visitors who arrive at your site from specific URLs. You might want to tell visitors from Digg to digg you again or maybe you want to give a big welcome to visitors from Google and a big kick up the ass to visitors from Yahoo! (not that I have anything against Yahoo!). You might even want to serve different reciprocal links and ads to visitors from different sites.

Dynamically tailoring content to different visitors is easier than you might think.

At the bottom of this page is a downloadable “Tailored Referer Message” script which can be configured to display up to three different messages or items to visitors depending on their entry route to a web page. An unlimited number of referring URL’s to match against and messages to display for each instance may be added to the script.

Before we get to the downloadable script, here’s a quick explanation of how we might check for the web page or domain that an incoming visitor has arrived from and how we might use that information to display content tailored to visitors from specific sites.

A few smart asses might notice the different spellings used for referrer (referer). The incorrect “referer” spelling is used in php. I’ve just followed the pattern :p

Checking the Referer

When someone clicks a link on a webpage to go to another site, the web browser passes the URL of the site being left onto the server of the destination site. The source site is called the referrer and the information sent to the destination site is called the HTTP Referrer. The HTTP Referrer may be read by php and the information it provides may be used to set the content we want to display on a web page.

The php variable $_SERVER[‘HTTP_REFERER’] can be used to read a visitor’s HTTP Referrer. That information may be written to the screen with the following instruction:

<?php

$ref=$_SERVER['HTTP_REFERER'];
echo "$ref";

?>

Try it!

Create two pages on your server. One called click.shtml and another called ref.php. Paste the above code into ref.php and save the following line into click.shtml

<a href="ref.php">Click here!</a>

Then open click.shtml in your web browser and follow the instructions.

Use the same two pages to play with the rest of the code written below here. Paste code snippets into ref.php.

We can strip the referrer information down to its domain and tld (e.g from http://google.com to google.com) with

<?php

parse_url($_SERVER['HTTP_REFERER'] , PHP_URL_HOST);

?>

Searching the referrer information for a specific data string is just as easy as printing it to the screen.

To display a message to visitors from a set URL, we need to compare the content of $_SERVER[‘HTTP_REFERER’] with a known domain. We can do that with preg_match(). We would look for google.com with a query like this:

<?php

preg_match("/google.com/i" , "$_SERVER['HTTP_REFERER']");

?>

preg_match() will return either a TRUE or FALSE result. Either the domain we have asked it to find is present in $_SERVER[‘HTTP_REFERER’] or it isn’t.

The “i” switch in the above snippet is important: it makes the search case insensitive.

Now we need to do something with the result

We could add an IF statement and print a messages to the screen if our site’s visitor has come from a particular site, such as Google:

<?php

if (preg_match("/google.com/i" , $_SERVER['HTTP_REFERER']))
{
echo "Welcome in from Google!";
}

?>

Putting it all together, we have a short script that will check a visitor’s referer against one specific domain:

<?php

$ref=$_SERVER['HTTP_REFERER'];
$domain_search = "google.com";
$ref_site = parse_url($ref, PHP_URL_HOST);

if (preg_match("/$domain_search/i" , $ref_site))
{
echo "Hello Google visitor!";
}

?>

What if we want to match against many different referrers?

…and display individual messages to visitors from each one?

This is a little trickier. We need to use an array.

An array is just a table of information. The php array function is written array().

A “table” of information is written into the array between the brackets. Arrays come in many different forms and one containing a list of referrer URLs and associated messages to print to the screen would look similar to this:

<?php

$data_array = array
(
"Hello visitor from Google!" => "google.com",
"Hello visitor from Bing!" => "bing.com",
"Hello visitor from Yahoo!" => "yahoo.com"
);

?>

The data on the right of our example array is called the “key”. Matching a search string with the data on the right unlocks access to the data to the left of the matched data.

To check whether an array contains a specific key data string, we use in_array() and we use array_search() to find the data associated with a particular key. For example,

in_array("google.com" , array());

would look for “google.com” in an array. The result will be TRUE if it is found or FALSE if it isn’t found.

Searching our example array for google.com with array_search()

array_search("google.com" , array());

would return the string “Hello visitor from Google!” because google.com is the key that unlocks that message in our array.

Using in_array() and array_search() with an “if” statement enables us to print a tailored message to visitors from URLs that match referrer URLs listed in our array. For example,

<?php

if (in_array("google.com" , array()))
{
$message = array_search("google.com" , array());
}

?>

Putting that all into one script, we have something that will search a list of domains (with tld)  for the domain.tld that an incoming visitor was on just before entering our site:

<?php

$ref=$_SERVER['HTTP_REFERER'];
$ref_site = parse_url($ref, PHP_URL_HOST);

$data_array = array
(
"Hello visitor from Google!" => "google.com",
"Hello visitor from Bing!" => "bing.com",
"Hello visitor from Yahoo!" => "yahoo.com"
);

if (in_array("$ref_site" ,  $data_array))
{
echo array_search("$ref_site" , $data_array);
} else {
echo "Hello visitor!";
}

?>

Play with the above script by pasting it into ref.php and changing “google.com” to “your-site.tld” (whatever your domain.tld is). Remember to change “Hello visitor from Google!” to something special to you.

Messages Can be Styled

Regular HTML may be used within php statements.

Instead of displaying a message, you could easily display an image, a form or even a completely different page to visitors from specific URLs. The data array in the above example script could easily be edited to display a table when a visitor arrives from Google or it could display a link to another site when someone arrives from Bing. For example:

$data_array = array
(
"<table><tr><td>One</td></tr><tr><td>Two</td></tr></table>" => "google.com",
"<a href=\"https://journalxtra.com\">Click for JX</a>" => "bing.com",
"<iframe src='https://journalxtra.com'>https://journalxtra.com</iframe>" => "yahoo.com"
);

Notice that I’ve used a backslash in front of double quotes in the second line and single quotes in place of double quotes in the third line where the data is between double quotes. This is to prevent the php parser from interpreting double quotes as the closing counterpart to the opening double quote that marks the data input area. Bit of a mind twister but you’ll work out what I mean by inspecting the above array.

Be Careful!

Referrer information can be manipulated by user agents such as web browsers, bots and firewalls. It can be removed or spoofed; and moving between https and http protocols will prevent referrer information from being passed to a server.

Do not trust referrer information and have a backup plan should the referrer be absent.

Downloadable Script

I’ve written a configurable php script which will display up to three items to a visitor. One of those items can be determined by referrer information. All of the items displayed may be explicitly stated else set to be randomly chosen from the data in the customizable array. The layout of the items printed to the screen can be set to horizontal, vertical or customized. You may use the script on commercial and non-commercial sites (but I’m not responsible for any resultant issues). Example uses for it are:

  • to display reciprocal links
  • to display a welcome message
  • to display images
  • to target ads to visitors from specific domains

It is fully annotated so configuring the settings and adapting it for other purposes should be easy.

It can be put into a WordPress site via the Shortcode Exec PHP plugin. Remember to remove the opening <?php and closing ?> tags if you use it this way.

I’m interested in seeing where and how you use the script so please give your site a mention in the comments.

[wpdm_file id=”11″]

Sharing is caring!

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
4
0
Would love your thoughts, please comment.x
()
x