我位于透明代理后面,该代理始终添加 HTTP_X_FORWARDED_FOR header 。
当我使用 PHP 转储 header 时, header 丢失了。但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=共享' '--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=共享' '--with-pdo-sqlite=共享' '--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 does not support mod_python but I'm on a dedicated box, so I can modify the configs if I only knew how.. :)
最佳答案
您正在使用 FastCGI。 PHP 对此并不满意,尽管它正在取得进展。 PHP 5.3 带来了 php-fpm,5.4 将带来 FastCGI 的 HTTP header 。请参阅 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}]
关于php - PHP 中缺少 $_SERVER header ,但 Python 中存在 $_SERVER header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7141566/