4

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

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

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

Now that this issue has come up, we're working on eliminating the use of $_SERVER['SERVER_NAME'] in future releases of the software ... but is there any configuration I can perform (in IIS6, php.ini, ... ?) on this server to fix this in the meantime? If we can't change it so that $_SERVER['SERVER_NAME'] always contains the host from the requesting URL, is there at least some way to configure it so that $_SERVER['SERVER_NAME'] will contain a particular desired FQDN ('app.foo.com' instead of 'myserver')?

EDIT: Added a bounty as I am very interested in receiving an answer to this question.

superphonic
  • 7,954
  • 6
  • 30
  • 63
mercurial
  • 5,203
  • 4
  • 26
  • 19
  • 1
    You could look at `$_SERVER["HTTP_HOST"]` with the **big** caveat that that is being set by the client and can be freely tampered with. Maybe if you have an array of valid hosts (if that doesn't defeat the purpose of what you are doing)... – Pekka Feb 16 '11 at 18:24
  • 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. You might need to revise your IIS virtual host setting – ajreal Feb 16 '11 at 18:35
  • Don't know about IIS, but on Apache, $_SERVER['SERVER_NAME'] is the value set in that vhost's "ServerName" httpd.conf option. IIS must have something similar to set an actual name for the vhost. – Marc B Feb 16 '11 at 18:36
  • I wonder if this question would get better answers on ServerFault? – vascowhite Nov 18 '14 at 09:58
  • Just out of curiosity, did you check the content of `%systemroot%\system32\drivers\etc\hosts` file? Your new server might have host name overwritten there. – Aleksei Matiushkin Nov 19 '14 at 15:49

4 Answers4

2

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

The globals such as $_SERVER are actually writable, so as a short term solution just to get things working, you could insert some quick PHP code to specifically set the SERVER_NAME key to the value you need for the site to work.

For example, in your opening PHP file, you could just include the line:

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

All subsequent calls to $_SERVER['SERVER_NAME'] would have the value you wanted.

If you needed to account for IP access, you could use a combination of REQUEST_URI, parse_url(), or HTTP_HOST if available.

Longterm, getting rid of SERVER_NAME from the code base will probably help reduce your blood pressure :)

AdamJonR
  • 4,703
  • 2
  • 22
  • 24
0

well as far as i heard, $_SERVER['SERVER_NAME'] gives the value that defined in the server configuration file and doesn't tell you anything about the request. Whereas, $_SERVER['HTTP_HOST'] gives you the domain name through which the current request is being fulfilled and is more directly related to the request.

Below is an example to clear more about this two things. Assume that you have a host defined in the Server with ServerName of domain.com and an IP address of 198.16.120.100.

Below are the different 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 guess this link will help you.

If you need anything more, do not hesitate.

Regards

Community
  • 1
  • 1
  • Unfortunately this isn't correct. I have several bog standard 2008 VMs, as well as several 2008 Azure Cloud instances(unmodified) that all return the host used to access the server when calling `$_SERVER['SERVER_NAME']`. For example should I access the server via `domain1.com`, I get `domain1.com` back, `domain2.com` I get `domain2.com` back etc... The reason for my bounty on this question(which isn't my question) is to find out from an authoritative source what configuration causes this behaviour, and what causes it to return the actual server name. – superphonic Nov 16 '14 at 22:11
  • @superphonic bro did you checked configuration files of ur IIS? Is everything ok in there? – William Francis Gomes Nov 17 '14 at 04:34
  • As far as I can tell. Like I say several machines are freshly spun up 2008 web roles on Azure Cloud. Configured as Microsoft intended, no messing from me. – superphonic Nov 17 '14 at 08:41
  • @superphonic bro i think u shud give a check of what the value of $_SERVER['SERVER_NAME'] set in the config file. As this value comes from config files. – William Francis Gomes Nov 19 '14 at 06:03
0

Please try to set host headers in IIS 6.0 and test it Reference

Note: $_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the CGI/1.1 specification, so you should be able to expect those.

The SERVER_NAME variable MUST be set to the name of the server host to which the client request is directed. It is a case-insensitive hostname or network address. It forms the host part of the 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 several HTTP virtual hosts share the same IP address. In that case, the server would use the contents of the request's Host header field to select the correct virtual host.

Arun Prakash
  • 1,717
  • 17
  • 26
0

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

mfonda
  • 7,873
  • 1
  • 26
  • 30