PHP中的$_SERVER头缺失,而Python中存在
我在一个透明的代理后面,这个代理总是会添加一个叫做HTTP_X_FORWARDED_FOR的头信息。
当我用PHP查看这些头信息时,发现这个头信息不见了。不过用Python就可以看到,哈哈。
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版本: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服务器API:CGI/FastCGI
Python运行方式:CGI
Hostgator不支持mod_python,但我在一个专用服务器上,所以如果我知道怎么修改配置就好了.. :)
1 个回答
你正在使用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}]