$_SERVER标头在PHP中不存在,但在Python中存在


$_SERVER headers are missing in PHP but present in Python

我背后的透明代理总是添加HTTP_X_FORWARDED_FOR头。

当我使用PHP转储报头时,报头丢失了。但是Python可以工作…lol

Python:

remote_addr = os.environ.get('HTTP_X_FORWARDED_FOR', os.environ.get('REMOTE_ADDR', ''))
PHP:

print_r($_SERVER['HTTP_X_FORWARDED_FOR']);
print_r($_SERVER['REMOTE_ADDR']);

----编辑:在------下面添加了更多关于配置的信息

PHP Version: 5.2.16

PHP配置命令:

'./configure' '--enable-bcmath' '--enable-calendar' '--enable-exif' '--enable-fastcgi' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-mbstring' '--enable-pdo=shared' '--enable-soap' '--enable-sockets' '--enable-sqlite-utf8' '--enable-wddx' '--enable-zip' '--prefix=/usr' '--with-curl=/opt/curlssl/' '--with-freetype-dir=/usr' '--with-gd' '--with-gettext' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/opt/openssl' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libexpat-dir=/usr' '--with-libxml-dir=/opt/xml2' '--with-libxml-dir=/opt/xml2/' '--with-mcrypt=/opt/libmcrypt/' '--with-mhash=/opt/mhash/' '--with-mime-magic' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-mysqli=/usr/bin/mysql_config' '--with-openssl=/opt/openssl' '--with-openssl-dir=/opt/openssl' '--with-pcre-regex=/opt/pcre' '--with-pdo-mysql=shared' '--with-pdo-sqlite=shared' '--with-png-dir=/usr' '--with-pspell' '--with-sqlite=shared' '--with-tidy=/opt/tidy/' '--with-ttf' '--with-xmlrpc' '--with-xpm-dir=/usr' '--with-xsl=/opt/xslt/' '--with-zlib' '--with-zlib-dir=/usr'

PHP Server API: CGI/FastCGI

Python Running as: CGI

Hostgator不支持mod_python,但我在一个专用的盒子上,所以我可以修改配置,如果我只知道如何…:)

您正在使用FastCGI。PHP在这方面并不轻松,尽管它正在取得进展。在PHP 5.3中出现了PHP -fpm,在5.4中出现了FastCGI的HTTP头。查看apache_request_headers或getAllHeaders()的变更日志

虽然没有纯PHP解决方案,但前面链接的第一个注释包含了一个解决方案:使用mod_rewrite来设置环境变量。

# Apache config for mod_rewrite
# from callum85 at notspam dot msn dot com 19-Apr-2007 06:07
RewriteEngine on
RewriteRule .* - [E=HTTP_X_FORWARDED_FOR:%{HTTP_X_FORWARDED_FOR}]