PHP中的变量$_SERVER与$HTTP_SERVER_VARS区别

原创 zhaoliang  2015-08-01 21:58  阅读 3,022 views 次

在编写网络程序中经常经常会用到$_SERVER,发现有些代码中会提及$HTTP_SERVER_VARS,可是在使用过程中发现$HTTP_SERVER_VARS会出现提示(Notice:   Undefined   variable:   HTTP_SERVER_VARS...),查了些资料,终于弄明白了。

原来,$_SERVER,$_ENV,$_COOKIE,$_GET,$_POST,$_FILES,$_SESSION在PHP 4.1.0及之后的PHP版本中才开始使用,在PHP4.1.0之前分别使用$HTTP_SERVER_VARS,$HTTP_ENV_VARS,$HTTP_COOKIE_VARS,$HTTP_GET_VARS,$HTTP_POST_VARS,$HTTP_POST_FILES,$HTTP_SESSION_VARS,也就是说在PHP 4.1.0及4.10之后,已经不再用$HTTP_*_VARS或HTTP_POST_FILES了。

$_SERVER 是一个包含诸如头部(headers)、路径(paths)和脚本位置(script locations)的数组。数组的实体由 web 服务器创建。不能保证所有的服务器都能产生所有的信息;有些环境中服务器可能忽略了一些信息,或者产生了一些未在标准中列出的新的信息。大多数的这些变量在 CGI 1.1 specification 中说明,所以您应该仔细研究它。这是一个“superglobal”,或者可以描述为自动全局变量。这只不过意味这它在所有的脚本中都有效。在函数或方法中您不需要使用 global $_SERVER; 访问它,就如同使用 $HTTP_SERVER_VARS 一样。 $HTTP_SERVER_VARS 包含着同样的信息,但是不是一个自动全局变量。(注意: $HTTP_SERVER_VARS 和 $_SERVER 是不同的变量,PHP 处理它们的方式不同。)

附上对应的英文原文(
Originally Posted by manual
$_SERVER
Note: Introduced in 4.1.0. In earlier versions, use $HTTP_SERVER_VARS.

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. You don't need to do a global $_SERVER; to access it within functions or methods, as you do with $HTTP_SERVER_VARS.

$HTTP_SERVER_VARS contains the same initial information, but is not an autoglobal. (Note that $HTTP_SERVER_VARS and $_SERVER are different variables and that PHP handles them as such)

If the register_globals directive is set, then these variables will also be made available in the global scope of the script; i.e., separate from the $_SERVER and $HTTP_SERVER_VARS arrays. For related information, see the security chapter titled Using Register Globals. These individual globals are not autoglobals.

You may or may not find any of the following elements in $_SERVER. Note that few, if any, of these will be available (or indeed have any meaning) if running PHP on the command line.

本文地址:http://blog.58cm.cn:8088/archives/106.html
版权声明:本文为原创文章,版权归 zhaoliang 所有,欢迎分享本文,转载请保留出处!

评论已关闭!