Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: $_SERVER variables: $_SERVER['HTTP_HOST'] vs $_SERVER['SERVER_NAME'] [duplicate]

Tags:

php

Possible Duplicate:
HTTP_HOST vs. SERVER_NAME

What is the difference between $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME'] ??

like image 369
Eman Avatar asked Dec 08 '12 00:12

Eman


People also ask

What is $_ server [' HTTP_HOST '] in PHP?

$_SERVER['HTTP_HOST'] Returns the Host header from the current request. $_SERVER['HTTP_REFERER'] Returns the complete URL of the current page (not reliable because not all user-agents support it)

What is HTTP_HOST?

The HTTP_HOST is obtained from the HTTP request header and this is what the client actually used as "target host" of the request. The SERVER_NAME is defined in server config.

What is $_ server [' Script_name ']?

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server.

What is $_ server [' Php_self ']?

$_SERVER['PHP_SELF'] Contains the file name of the currently running script. $_SERVER['GATEWAY_INTERFACE'] Contains the version of the Common Gateway Interface being used by the server.


2 Answers

$_SERVER['SERVER_NAME'] gives the value of the server name as defined in host configuration (i.e for Apache the Apache .conf file).

$_SERVER['HTTP_HOST'] gives you the domain name through which the current request is being fulfilled and is more directly related to the request.

HTTP_HOST is typically more useful in most applications in that it relates directly to the request, whereas SERVER_NAME could return whatever value is in the conf file and doesn't tell you anything about the request at all.

I will give you an example of how HTTP_HOST might differ from SERVER_NAME. Say you have an host defined in Apache with ServerName of example.com and an IP address of 1.2.3.4.

Let's look at two incoming request URLs and show the difference between these variables:

http://www.example.com
HTTP_HOST = www.example.com
SERVER_NAME = example.com

http://1.2.3.4
HTTP_HOST = 1.2.3.4
SERVER_NAME = example.com

So again, HTTP_HOST is tied more to the request, whereas SERVER_NAME is determined by server configuration.

like image 51
Mike Brant Avatar answered Oct 21 '22 00:10

Mike Brant


HTTP_HOST is the Host: header sent by the client. As a result, it might be a little less trustworthy. SERVER_NAME is determined by your server's configuration, regardless of user input.

The difference in behavior is subtle. Some good examples are demonstrated here: http://shiflett.org/blog/2006/mar/server-name-versus-http-host

The docs explain this well

'SERVER_NAME' The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

'HTTP_HOST' Contents of the Host: header from the current request, if there is one.

like image 24
Frank Farmer Avatar answered Oct 20 '22 23:10

Frank Farmer