php - PHP 中缺少 $_SERVER header ,但 Python 中存在 $_SERVER header

标签 php python apache ip

我位于透明代理后面,该代理始终添加 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/

相关文章:

php - Ajax - 将内容加载到 Textarea 中

PHP mkdir 问题!

python - 使用正则表达式查找与特定字符串匹配的文本

python - pip 不安装在 setup() 中声明的依赖项需要参数

apache - Magento-Varnish 最佳 VCL 配置

php - COUNT/GROUP BY 查询不起作用

php - PHP |更新查询给我 undefined index :

python - GTK3 中 TextView 小部件的文本更改信号

php - puppet : How to stop from PHP code printing in browser window

java - ApacheConnectorProvider : Jersey Client 2. 5.1