Well my question is this:
I have multiple domains that all point to the same location on my site:
www.example1.com www.example2.com www.example3.com
How do I check with PHP, which domain is the user using to navigate my site?
Thank you.
Well my question is this:
I have multiple domains that all point to the same location on my site:
www.example1.com www.example2.com www.example3.com
How do I check with PHP, which domain is the user using to navigate my site?
Thank you.
The global variable $_SERVER
contains the information you need.
The current domain can be obtained at index HTTP_HOST
or SERVER_NAME
.
Note that these are data that depends on the environment settings, ie the operating system settings and especially the web page server (apache, iis, nginx, etc.).
So do not fully trust these variables. Always check integrity when changing environment.
Without further ado, an example of how to get what you want:
echo $_SERVER['HTTP_HOST'];
or
echo $_SERVER['SERVER_NAME'];
Note also that $_SERVER['HTTP_HOST']
can return the port number concatenated to the host name.
Example localhost:81
Also check other indexes of array $_SERVER
:
$_SERVER['SERVER_ADDR']
$_SERVER['HTTP_X_FORWARDED_HOST']
Just in case, just print_r($_SERVER)
and see where the hostname appears.
print_r($_SERVER);
Remembering that the host name is independent of DNS, so in certain situations the host name may return something that has nothing to do with the DNS domain name.
In conclusion, do not fully trust this data.
The correct thing is that the hosting server has the default structure of the domain name also defined as the name of the virtual host. But many servers, mostly shared, do not adopt this rule, and may have names that are unrelated to the DNS domain.