Ash Ash -3 years ago 115
Apache Configuration Question

$_SERVER headers are missing in PHP but present in Python

I'm behind a transparent proxy which always adds the HTTP_X_FORWARDED_FOR header.

When I dump the headers out using PHP the header is missing. But Python works.. 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']);


---- EDIT: Added more info on configs below ------

PHP Version:
5.2.16


PHP Configure Command:

'./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 does not support mod_python but I'm on a dedicated box, so I can modify the configs if I only knew how.. :)

Answer Source

You are using FastCGI. PHP isn't at ease with this, though it's making progress. With PHP 5.3 came php-fpm, with 5.4 will come HTTP headers for FastCGI. See the changelog of apache_request_headers or getAllHeaders().

Though there's no pure PHP solution, the first note of the previous link contains a workaround: using mod_rewrite to set up environment variables.

# 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}]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download