I have read the first comment here which said:
All elements of the $SERVER array whose keys begin with 'HTTP' come from HTTP request headers and are not to be trusted.
As well as this answer which said:
$_SERVER["HTTP_HOST"] is the HTTP Host header, as sent from the client. That makes this header generally unsafe.
They are all saying that $_SERVER['HTTP_HOST']
is not safe, BUT there is only one alternative so far as I can see which is $_SERVER['SERVER_NAME']
.
$_SERVER['SERVER_NAME']
is also not safe which is highlighted in the official doc:
Note: Under Apache 2, UseCanonicalName = On and ServerName must be set. Otherwise, this value reflects the hostname supplied by the client, which can be spoofed. It is not safe to rely on this value in security-dependent contexts.
So, my question is that there is really no alternative ( considered safe ) to $_SERVER['HTTP_HOST']
? Otherwise, I have to use it anyway even though I know it is not safe because I have no choice...
Edited: My purpose is to get the base URL. And I don't have control because this is in a plugin for users who might misconfigure Apache.