Get only the Domain Name from HTTP_REFERRER in PHP


$_SERVER['HTTP_REFERER'] gives you the URL from where the user came, which is the user’s previous page (whatever page that would appear if the user clicked “Back” in their browser)

But $_SERVER['HTTP_REFERER']  gives you the complete URL, something like this :

driverstest.info/test/quiz.php?quizgroup=7

If you just want get the domain name .i.e. driverstest.info from the full URL, then you can use the following code:

$referer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);

Note though that the referer is easy to spoof, so it’s not reliable.


Leave a Reply

Your email address will not be published. Required fields are marked *