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.

Nathan Bunn
  • 597
  • 1
  • 7
  • 14

4 Answers4

60
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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 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. – Nathan Bunn Jan 29 '12 at 01:14
  • By the way, how easy are URLs to spoof? Could someone easily spoof my domain? How? – Nathan Bunn Jan 29 '12 at 01:15
  • 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. – deceze Jan 29 '12 at 01:23
  • 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. – Nathan Bunn Jan 29 '12 at 01:39
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.

Dan LaManna
  • 3,431
  • 4
  • 23
  • 35
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)
Tony
  • 11
  • 1
-1

this should do :

 $_SERVER['SERVER_NAME']
Matoeil
  • 6,851
  • 11
  • 54
  • 77
  • 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 domain_ – Cassandra Sep 07 '16 at 05:37