Today one student from the Web Development class asked me “how web sites are detecting referrals?”, this is a nice question and though of giving the answer publicly so that anyone can get the benefit.
First of all will discuss what this is?
May be you have experienced when you visit to a web site it says where you come from, for an instance assume that you click on a Google search then the site knows that you come through Google. This is a very important thing in SEO as well.
The code behind is very very simple. What you have to do is just use the ‘HTTP_REFERER’ global variable in the code.
You can find an example below.
$referer = $_SERVER[‘HTTP_REFERER’];
echo “You have came though <br/> ” . $referer;
The output would be the URL which is clicked before you come to the site where the above PHP code is.
Bare mind that the out put is a full qualified URL which has http:// or the protocol in front of the URL. Depending on your imagination this code snippet can be used in various applications.