How do I select only the root domain from $_SERVER['HTTP_REFERER'];?
Asked Answered
N

4

30

I want to have a 404 page that can detect if a user has come to that page either via my site, via my shortened URL or via another site, and I am making it using PHP. I am slowly getting to grips with PHP and this may well be a simple question, but I am quite tired to be honest and have no caffeine in my system, and I am wanting to tie up any loose ends in my portfolio as soon as possible.

I have found the $_SERVER['HTTP_REFERER']; PHP variable gives me the entire URL, which is a start. Is there a way that this can give me only the root domain, either via another variable or a function, bearing in mind that some referrers may be using http:// and some https:// (so simply starting from the seventh character would not always work)? That way, I can match the URL based on two (or more) predefined addresses and produce the content that relates to that domain.

Novelty answered 29/1, 2012 at 0:52 Comment(0)
M
61
parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST)

See http://php.net/manual/en/function.parse-url.php.

Note though that the referer is terrifically easy to spoof, so it's hardly reliable.

Maritsa answered 29/1, 2012 at 0:55 Comment(4)
That's wonderful! That works perfectly, apart from the fact that I can't do the same with 301 redirected URLs, which I think may well cause issues with my URL shortener. No matter, I will find a way.Novelty
By the way, how easy are URLs to spoof? Could someone easily spoof my domain? How?Novelty
I'm saying the referer is easy to spoof. It's just arbitrary, optional, voluntary data send in the HTTP header of the request. The client can send anything it wants there. There are browser plugins that will change the referer to anything you like.Maritsa
Oooooh, sorry, thank you for explaining that to me again. I'm not too worried about that to be honest, since it is for data being displayed on a 404 page and not being recorded, but thank you anyway. I was not aware of that, so thanks for letting me know.Novelty
A
3

http://php.net/manual/en/function.parse-url.php may be your best bet, although you could use a regular expression to achieve what you're looking for easily as well.

Accumulate answered 29/1, 2012 at 0:55 Comment(0)
A
0

Do not use a regexp or parse_url as it's consumpt much resources better use

trim(substr($_SERVER['HTTP_REFERER'], strpos($_SERVER['HTTP_REFERER'], '/')+2), '/')

or if referrer always have a slash at the end:

substr($_SERVER['HTTP_REFERER'], strpos($_SERVER['HTTP_REFERER'], '/')+2, -1)
Autobahn answered 5/2, 2023 at 12:26 Comment(0)
M
-1

this should do :

 $_SERVER['SERVER_NAME']
Marras answered 7/1, 2016 at 9:29 Comment(1)
Not really a very good option as if the you use a subdomain like potato.domain.com and you want to be getting domain.com as your result, this will fail. The OP did state root domainVictualler

© 2022 - 2024 — McMap. All rights reserved.