5

When I check the logs there are a few rare occasions where for some visitors(bot I think) the $_SERVER['HTTP_HOST'] is returning: www.domain.co.uk:80

What is the correct way to get only the domain name without any ports?

Should I be doing it this way instead:

$url_components = parse_url($_SERVER['HTTP_HOST']);
$domain = $url_components['host'];

The strange thing is that I cant replicate this behavior it only appears in the logs.

John Magnolia
  • 16,769
  • 36
  • 159
  • 270

1 Answers1

11

In this case $_SERVER['SERVER_NAME'] is your friend.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • Oh I was thinking that HTTP_HOST was more reliable obviously wrong after reading: http://stackoverflow.com/a/2297421/560287 – John Magnolia Nov 01 '13 at 10:22
  • Both of them has their own purpose of strength. – Shankar Narayana Damodaran Nov 01 '13 at 10:26
  • This will not work if you have server names aliases. Ex (nginx) : server { server_name new.exemple.com www.exemple.com; ... } If you request for http://www.exemple.com/foo : In this case $_SERVER['HTTP_HOST'] is www.exemple.com and $_SERVER['SERVER_NAME'] is new.exemple.com – Julien Dec 05 '18 at 16:58