How to check the domain you are in

Asked

Viewed 34 times

1

Well my question is this::

I have several domains that all point to the same site on my site:

www.exemplo1.com www.exemplo2.com www.exemplo3.com

How do I check with PHP, which domain you are using to browse my site?

Thank you.

1 answer

1


The global variable $_SERVER contains the information you need.

The current domain can be obtained in the index HTTP_HOST or SERVER_NAME.

Note that it is data that depends on the settings of the environment, that is, the configurations of the Operating System and mainly of the WEB page server (apache, iis, Nginx, etc).

So do not fully trust these variables. Always check the 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 array indexes $_SERVER:

$_SERVER['SERVER_ADDR']
$_SERVER['HTTP_X_FORWARDED_HOST']

Just in case, just do one print_r($_SERVER) and see where the name of the host appears.

print_r($_SERVER);

Remembering that the host name is independent of DNS, so in certain situations the host name can return something that has nothing to do with the DNS domain name.

In conclusion, do not fully trust these data.

The most correct is that the hosting server has as its default structure the domain name defined as the virtual host name as well. But many servers, mostly shared, do not adopt this rule, and may have names that are unrelated to the DNS domain.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.