How to display referer-based contents in your web pages using PHP
In this article I’ll explain how to use PHP in order to offer your visitors targeted content based upon their referring page.

PHP and CSS allow you to create a customized box which appears in a defined place of your webpages and gives help to visitors who satisfy predetermined conditions: the PHP variable $_SERVER consists in an array containing several information. The variable $_SERVER[’HTTP_REFERER’] contains the referer URL (i.e. the website or webpage where the visitor comes from).
You can use this variable to offer a referer-based content to your visitors: for example when some visitors come from a particular URL, you can show them a box containing tips that help finding quickly what they are looking for.
Let’s go deeper into the matter and see a practical use of this technique…
First, create a css class:
.helper {
position:absolute;
top: 0px;
left: 0px;
background-color: #CC3300;
color:#FFFFFF;
}
I called the CSS class “.helper” and defined an element with absolute position (top-left), a red background and white text.
Now, you can use the following PHP code to display the “.helper” element defined in CSS on determined conditions. Using the PHP command eregi on the variable $_SERVER[’HTTP_REFERER’], the server can look for the presence of certain terms in the referer URL:
<?php
if (eregi("font“,$_SERVER[’HTTP_REFERER’])) {
echo “<div class=\”helper\”>Looking for YellowJug font?<br />
<a href=\”projects/yellowjug_true_type_font/\”>Click here…</a>
</div>”;
}
?>
The human “translation” of the code above is:
if the visitor’s origin is a URL which contains the term “font”, then display red a box on the top left of the webpage (the box position and colors are obviously defined in the CSS class .helper) which suggests the visitor to visit a certain link or page.
If the term-matching condition is not satisfied, the box simply won’t appear and won’t annoy your visitors.
To see this soluction in action just visit yellowjug.com homepage and check the top left corner: no help box should appear; then go to YellowJug font page on dafont.com and click on the link “Site” next to the author’s name (it’s me
). You’ll be redirected again to yellowjug.com home page but now you’ll be able to see the red box!
Please consider that this technical solution can be adopted on different approaches in more elaborate contexts. These “boxes” could offer hints, tips or suggestions and can be displayed ad hoc, for example, when a visitor comes from a certain page or comes from a page which contains a certain term (including search engines pages).

Leave a Reply