Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get domain name without port using HTTP_HOST

Tags:

php

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.

like image 383
John Magnolia Avatar asked Jan 13 '23 00:01

John Magnolia


1 Answers

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

like image 104
Shankar Narayana Damodaran Avatar answered Jan 16 '23 20:01

Shankar Narayana Damodaran