What's The Difference Between HTTP_HOST And SERVER_NAME In PHP?

Published October 28, 2024

Problem: Distinguishing HTTP_HOST and SERVER_NAME

PHP developers often get confused when using HTTP_HOST and SERVER_NAME. These two server variables look similar but have different uses and behaviors. Knowing their differences is useful for correct server identification and request handling in PHP applications.

HTTP_HOST: Client-Provided Information

Definition and Source

HTTP_HOST is a PHP server variable that contains the host name in the HTTP request header. It shows the domain name or IP address the client used to access the server. This value comes from the "Host" header field of the incoming HTTP request.

Characteristics of HTTP_HOST

HTTP_HOST is a client-controlled value, provided by the client's browser or application making the request. It shows the host name used in the request, which can be useful in some cases. However, since it's client-controlled, it can be changed or spoofed by the client, making it less reliable for security-sensitive tasks.

The HTTP_HOST value usually includes the domain name and port number (if specified) used in the request. For example, if a user visits "example.com:8080", the HTTP_HOST value would be "example.com:8080". This makes it useful for creating URLs that match the original request, especially in applications that may be accessed through different domain names or ports.

Tip: Validate HTTP_HOST

Always validate and sanitize the HTTP_HOST value before using it in your application. Use a whitelist of allowed hostnames or compare it against your expected domain names to prevent potential security issues.

SERVER_NAME: Server Configuration Data

Definition and Source

SERVER_NAME is a PHP server variable that contains the name of the server host running the current script. Unlike HTTP_HOST, SERVER_NAME is set in the server configuration, not by the client request. Web servers like Apache, Nginx, or IIS usually set this value based on their config files.

The SERVER_NAME value is often set in the server's config file. For example, in Apache, it's defined using the ServerName directive in the virtual host configuration. This setting lets server admins specify the main name for the server, which may differ from the hostname used in client requests.

Example: Apache Virtual Host Configuration

<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /var/www/example.com/public_html
    ...
</VirtualHost>

Characteristics of SERVER_NAME

SERVER_NAME is controlled by the server, meaning it's set by the server configuration and not affected by client requests. This makes it more reliable and secure than HTTP_HOST, especially for tasks that need a trusted server identifier.

As a server-defined value, SERVER_NAME usually stays the same regardless of how clients access the server. This consistency can help generate absolute URLs or identify servers in multi-host setups. However, the accuracy of SERVER_NAME depends on proper server configuration.

SERVER_NAME is used when a stable, server-defined hostname is needed, such as in security-sensitive operations or when creating canonical URLs. However, its behavior can change based on server software and configuration, so it's important to know how it's set in your specific server environment.

Tip: Verify SERVER_NAME Configuration

Always check your server configuration to confirm that SERVER_NAME is set correctly. Incorrect settings can lead to unexpected behavior in your applications, especially those relying on this variable for URL generation or server identification.

Comparing HTTP_HOST and SERVER_NAME

Key Differences

HTTP_HOST and SERVER_NAME differ in origin and reliability. HTTP_HOST comes from the client's request header, while SERVER_NAME is set in the server configuration. This affects their reliability and security implications.

HTTP_HOST changes based on the client's request, making it less reliable for security-sensitive operations. SERVER_NAME provides a consistent, server-defined value, making it more reliable but less adaptable to different access methods.

Tip: Verify HTTP_HOST

Always validate and sanitize the HTTP_HOST value before using it in your application to prevent potential security risks.

Use Cases for HTTP_HOST

HTTP_HOST is useful when you need to match the hostname used in the client's request. It helps with:

  • Creating dynamic links that match the accessed domain
  • Handling multiple domains pointing to the same application
  • Detecting the protocol (HTTP/HTTPS) used in the request

Potential risks of using HTTP_HOST:

  • It can be spoofed by malicious clients
  • It may not always contain the expected value
  • It might include unexpected ports or subdomains

Use Cases for SERVER_NAME

SERVER_NAME is better for situations that need a stable, server-defined hostname. It's useful for:

  • Generating canonical URLs
  • Identifying the server in multi-server setups
  • Security-sensitive operations that need a trusted hostname

Benefits of using SERVER_NAME:

  • Consistent values across requests
  • Higher reliability for server identification
  • Less vulnerability to client-side manipulation

Potential drawbacks of SERVER_NAME:

  • It may not reflect the actual hostname used in the request
  • Its value depends on correct server configuration
  • It might not work as expected in certain server setups or proxy environments

Example: Using SERVER_NAME for Email Headers

$from = "noreply@" . $_SERVER['SERVER_NAME'];
$headers = "From: $from\r\n";
mail($to, $subject, $message, $headers);

This example shows how to use SERVER_NAME to create a consistent "From" address for emails sent from your application.

Configuring SERVER_NAME in Web Servers

Apache Configuration

To set SERVER_NAME in Apache, use the ServerName directive in your server configuration file. This directive sets the hostname and port that the server uses to identify itself.

In Apache's configuration file (typically httpd.conf or apache2.conf), set the ServerName directive like this:

ServerName www.example.com:80

This tells Apache to use "www.example.com" as the server name when responding on port 80.

The UseCanonicalName directive affects how Apache builds self-referential URLs. When set to "on", Apache uses the hostname and port specified in the ServerName directive. Set it like this:

UseCanonicalName on

This makes SERVER_NAME always return the value specified in ServerName, regardless of the client's request.

Using ServerAlias for Multiple Domains

If you want your Apache server to respond to multiple domain names, use the ServerAlias directive in addition to ServerName:

ServerName www.example.com
ServerAlias example.com example.net example.org

This configuration allows your server to respond to requests for all listed domains.

Other Web Servers

For Nginx, set the server_name directive in the server block of your configuration file:

server {
    server_name example.com www.example.com;
    # other configuration settings
}

In Microsoft IIS, set the server name through the IIS Manager interface or in the applicationHost.config file:

<site name="Default Web Site" id="1">
    <bindings>
        <binding protocol="http" bindingInformation="*:80:www.example.com" />
    </bindings>
    <!-- other settings -->
</site>

For LiteSpeed Web Server, set the server name in the virtual host configuration:

vhName                  example.com
vhAliases               www.example.com

Restart your web server after making changes to the configuration to apply the new settings.