Last active
August 29, 2015 14:03
-
-
Save jrenggli/738ceb8820344651e270 to your computer and use it in GitHub Desktop.
Adjust request headers when using proxy servers (former https.php)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Detects a Request through HTTP/HTTPS Proxy and sets the coresponding server variables. | |
* | |
* usage: | |
* in php.ini or .htaccess | |
* php_value auto_prepend_file /var/www/proxy.php | |
* | |
* in pool.d for php-fpm | |
* php_value[auto_prepend_file] = /var/www/proxy.php | |
* | |
*/ | |
// | |
// Detect SSL | |
// | |
if (array_key_exists('HTTP_SSL_ON', $_SERVER) && $_SERVER['HTTP_SSL_ON']) { | |
$ssl = true; | |
} elseif (array_key_exists('HTTP_HTTP_SSL_ON', $_SERVER) && $_SERVER['HTTP_HTTP_SSL_ON']) { | |
$ssl = true; | |
} elseif (array_key_exists('HTTPS', $_SERVER) && ($_SERVER['HTTPS'] === 'on') || ($_SERVER['HTTPS'] === 1)) { | |
$ssl = true; | |
} elseif (array_key_exists('HTTP_X_FORWARDED_PROTO', $_SERVER) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { | |
$ssl = true; | |
} else { | |
$ssl = false; | |
} | |
unset($_SERVER['HTTP_SSL_ON']); | |
unset($_SERVER['HTTP_HTTP_SSL_ON']); | |
unset($_SERVER['HTTP_X_FORWARDED_PROTO']); | |
$_SERVER['HTTPS'] = $ssl ? 1 : 0; | |
$_SERVER['SERVER_PORT'] = $ssl ? '443' : $_SERVER['SERVER_PORT']; | |
// | |
// Detect Host | |
// | |
if (array_key_exists('HTTP_X_FORWARDED_HOST', $_SERVER)) { | |
$host = $_SERVER['HTTP_X_FORWARDED_HOST']; | |
} else { | |
$host = $_SERVER['HTTP_HOST']; | |
} | |
unset($_SERVER['HTTP_X_FORWARDED_HOST']); | |
$_SERVER['HTTP_HOST'] = $host; | |
// | |
// Detect Remote Addr | |
// | |
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) { | |
$remoteAddr = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} else { | |
$remoteAddr = $_SERVER['REMOTE_ADDR']; | |
} | |
unset($_SERVER['HTTP_X_FORWARDED_FOR']); | |
$_SERVER['REMOTE_ADDR'] = $remoteAddr; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment