Web Dev - Knowing Where Visitors Came From

If you need to customize your web page based on how your visitors found your web page, there is a very simple way: it's called the HTTP Referer header field. Note that "referer" is a misspelling of "referrer" which has stuck around.

The following PHP code shows the referrer, if it exists:

<?php
   $ref = $_SERVER['HTTP_REFERER'];
?>
<!DOCTYPE html>
  <body>
    <?php echo $ref; ?>
 </body>
</html>

To check the referrer, you must use another file that links to it, which in this case sits in the same directory.

<!DOCTYPE html>
  <body>
     <a href="examplereferrer.php">Click ME!</a>
 </body>
</html>

You can apply the same principle to any language, as this is provided by the HTTP specification. As long as the client sends you this information, you can use. Remember it is optional, so you may end up with an empty variable.