$_SERVER['HTTP_REFERER']
will give you the referrer page's URL if there exists any. In case of direct access (from a bookmark or user entering the url directly) you won't get anything.
Using HTTP_REFERER in PHP
The HTTP REFERER in PHP is stored in the $_SERVER super global, and can be referenced from anywhere in your PHP code like in the following example, which would simply write it out to the browser:
echo $_SERVER['HTTP_REFERER'];
If the HTTP_REFERER has been set then it will be displayed. If it is not then you won't see anything. If it's not set and you have error reporting set to show notices, you'll see an error like this instead:
Notice: Undefined index: HTTP_REFERER in /path/to/filename.php on line 3
To prevent this error when notices are on (I always develop with notices on), you can do this:
if(isset($_SERVER['HTTP_REFERER']))
{
echo $_SERVER['HTTP_REFERER'];
}
In order to get the domain form the referrer you can use parse_url() function of PHP:
Example:
$url = '//www.example.com/path?name=shakti';
var_dump(parse_url($url));
You will get an output as:
array(3) {
["host"]=>
string(15) "www.example.com"
["path"]=>
string(5) "/path"
["query"]=>
string(17) "name=shakti"
}