The referer information is useful for a number of purposes, mostly in server statistics – it is commonly discouraged to rely on the referer for most other purposes, as it may or may not be present. Users can also disable the referer header in their browser, or fill it out with fake information.
The referer header can be easily retrived using the $_SERVER array, which usually also contains other information, such as the IP or User Agent string.
A Couple of Usage Tips
Often you need a little inspiration as of how a given feature of PHP can be used. Usually you wouldn't use the referer header for anything but statistic purposes – however, it can also be used to benefit your users.
When someone visits your website from a search engine, you could use the referer information to highlight the keywords or keyphrases that they used to find your site. You may however still want to be careful not to ruin the readability of your site, as this is not the expected behavior – the trick is not to highlight just every single keyword, but rather limit it to keyphrases.
Get Referer in PHP
The HTTP referer header should be secured or validated before being used critical places, as it can easily be changed by users.
The following PHP Get referer example demonstrates how you can return the HTTP referer header using PHP – the script uses the $_SERVER HTTP_REFERER Key, and simply echo's its content back to the browser.
<?php echo $_SERVER['HTTP_REFERER']; ?>
Saving the referer into a variable for later use throughout your PHP script:
<?php $referer = $_SERVER['HTTP_REFERER']; echo $referer; ?>