PHP $ _SERVER ['SERVER_NAME'] returns the machine name in IIS6

My company uses a piece of PHP-based software that depends on $_SERVER['SERVER_NAME'] to create the URL. It runs on PHP 5.2 under Windows Server 2003 or 2008 with IIS6 or IIS7 via FastCGI.

This works “correctly” (or at least as we expect it to work) on every IIS system in which we have ever installed it. In other words, on the same server, if you call it using http://app.foo.com/myscript.php , $_SERVER['SERVER_NAME'] is 'app.foo.com', if you call it using http://192.168.1.22/myscript.php , $_SERVER['SERVER_NAME'] - "192.168.1.22", etc.

Today, for the first time, we installed it on a server (Windows Server 2003 with IIS6), which acts differently. No matter what URL we use to load the script, $ _SERVER ['SERVER_NAME'] is "myserver" (server machine name), which causes problems.

Now that this problem has occurred, we are working to eliminate the use of $_SERVER['SERVER_NAME'] in future software releases ... but is there any configuration that I can do (in IIS6, php.ini, ... ?) on this server to fix this in the meantime? If we cannot change it so that $_SERVER['SERVER_NAME'] always contains the host from the requesting URL, is there any way to configure it, so $_SERVER['SERVER_NAME'] will contain the specific desired FQDN (' app.foo.com 'instead of' myserver ')?

EDIT: Added generosity as I am very interested in getting an answer to this question.

+4
source share
4 answers

but is there any configuration that I can do (in IIS6, php.ini, ...?) on this server to fix this in the meantime?

Globals, such as $ _SERVER, are actually writable, so for a short-term solution just to make things work, you can insert some quick PHP code to specifically set the SERVER_NAME key to the value necessary for the site to work.

For example, in your opened PHP file, you can simply include the line:

 $_SERVER['SERVER_NAME'] = 'app.foo.com'; 

All subsequent calls to $ _SERVER ['SERVER_NAME'] will have the desired value.

If you need to consider IP access, you can use a combination of REQUEST_URI, parse_url (), or HTTP_HOST, if available.

Long-term, getting rid of SERVER_NAME from the code base will probably help lower your blood pressure :)

+2
source

Try using $_SERVER['HTTP_HOST'] , or if that doesn't work, use $_SERVER['SCRIPT_URI'] and parse_url() .

0
source

as far as I heard, $_SERVER['SERVER_NAME'] gives the value defined in the server configuration file and does not report anything about the request. Whereas $_SERVER['HTTP_HOST'] gives you the domain name through which the current request is executed, and is more directly related to the request.

Below is an example to learn more about these two things. Suppose you have a host defined on a server with ServerName of domain.com and IP address of 198.16.120.100 .

The following are the different values ​​between these two variables:

 for http://www.domain.com $_SERVER['SERVER_NAME'] = domain.com $_SERVER['HTTP_HOST'] = www.domain.com for http://198.16.120.100 $_SERVER['SERVER_NAME'] = domain.com $_SERVER['HTTP_HOST'] = 198.16.120.100 

If you want to change SERVER_NAME of your IIS server, I think this link

0
source

Try setting host headers in IIS 6.0 and testing it. Help

Note. $_SERVER is an array containing information such as headers, paths, and script. The entries in this array are created by the web server. There is no guarantee that each web server will provide any of these; servers may omit some or provide to others not listed here. However, a large number of these variables are considered in the CGI / 1.1 specification , so you can expect them.

The SERVER_NAME variable MUST be set to the hostname of the server to which the client request is directed. This is a case-insensitive host name or network address. It forms the bulk of Script -uri.

  SERVER_NAME = server-name server-name = hostname | ipv4-address | ( "[" ipv6-address "]" ) 

A deployed server can have more than one possible value for this variable, where multiple HTTP virtual hosts use the same IP address. In this case, the server will use the contents of the Host header request to select the correct virtual host.

0
source

Source: https://habr.com/ru/post/1340060/


All Articles