Skip to content
PHP Logo

What are the potential pitfalls of using $_SERVER['SERVER_NAME'] to determine domain?

Using $_SERVER['SERVER_NAME'] to determine the domain can be unreliable as it relies on the server configuration, which may not always be accurate. It is better to use a combination of other server variables like $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_PORT'] to determine the domain more accurately.

php
$domain = $_SERVER['HTTP_HOST'] . ($_SERVER['SERVER_PORT'] != 80 ? ':' . $_SERVER['SERVER_PORT'] : '');