From 846fed2243c1d3a666019d273ae559a8d893f5d5 Mon Sep 17 00:00:00 2001 From: Jon Schewe Date: Mon, 23 Dec 2024 09:52:39 -0600 Subject: [PATCH] Add support for standard proxy server variables If x-forwarded-port or x-forwarded-proto are defined, use these headers when constructing URLs. --- system/helpers/request.php | 4 ++++ system/helpers/url.php | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/system/helpers/request.php b/system/helpers/request.php index 39e521927..0aa66ee37 100644 --- a/system/helpers/request.php +++ b/system/helpers/request.php @@ -69,6 +69,10 @@ public static function protocol() { return $config['site_protocol']; } + elseif ( ! empty($_SERVER['HTTP_X_FORWARDED_PROTO']) ) + { + return $_SERVER['HTTP_X_FORWARDED_PROTO']; + } elseif ( ! empty($_SERVER['HTTPS']) AND $_SERVER['HTTPS'] === 'on') { return 'https'; diff --git a/system/helpers/url.php b/system/helpers/url.php index e0c4f174e..b1b987b44 100644 --- a/system/helpers/url.php +++ b/system/helpers/url.php @@ -67,7 +67,14 @@ public static function base($index = FALSE, $protocol = FALSE) if ($site_domain === '' OR $site_domain[0] === '/') { // Guess the server name if the domain starts with slash - $port = $_SERVER['SERVER_PORT']; + if ( ! empty($_SERVER['HTTP_X_FORWARDED_PORT']) ) + { + $port = $_SERVER['HTTP_X_FORWARDED_PORT']; + } + else + { + $port = $_SERVER['SERVER_PORT']; + } $port = ((($port == 80) && ($protocol == 'http')) || (($port == 443) && ($protocol == 'https')) || !$port) ? '' : ":$port"; $base_url = $protocol.'://'.($_SERVER['SERVER_NAME']?($_SERVER['SERVER_NAME'].$port):$_SERVER['HTTP_HOST']).$site_domain; }