开发者

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

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 th开发者_开发技巧is question.


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 :)


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜