From dfd51cb0379936a5cc90b980ddf46b170f3b365b Mon Sep 17 00:00:00 2001 From: vlakoff Date: Mon, 31 Mar 2014 20:41:07 +0200 Subject: Refactoring in Config->__construct() Put the $_SERVER['HTTP_HOST'] fallback in a more logical place. --- system/core/Config.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'system/core/Config.php') diff --git a/system/core/Config.php b/system/core/Config.php index f630d1709..56259bd06 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -76,16 +76,9 @@ class CI_Config { // Set the base_url automatically if none was provided if (empty($this->config['base_url'])) { - if (isset($_SERVER['HTTP_HOST'])) - { - $base_url = is_https() ? 'https' : 'http'; - $base_url .= '://'.$_SERVER['HTTP_HOST'] - .str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); - } - else - { - $base_url = 'http://localhost/'; - } + $base_url = (is_https() ? 'https' : 'http') . '://' + . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') + . str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); $this->set_item('base_url', $base_url); } -- cgit v1.2.3-24-g4f1b From 2516f3b3945106aeebda87bd6a35454b7d72bdba Mon Sep 17 00:00:00 2001 From: vlakoff Date: Mon, 31 Mar 2014 20:46:04 +0200 Subject: Robuster determination of "base_url" config item Remove the basename only at the end, to avoid edge cases. --- system/core/Config.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'system/core/Config.php') diff --git a/system/core/Config.php b/system/core/Config.php index 56259bd06..728e28dd8 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -76,9 +76,11 @@ class CI_Config { // Set the base_url automatically if none was provided if (empty($this->config['base_url'])) { + $script_basename = basename($_SERVER['SCRIPT_NAME']); + $base_url = (is_https() ? 'https' : 'http') . '://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') - . str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); + . preg_replace('/'.preg_quote($script_basename).'$/', '', $_SERVER['SCRIPT_NAME']); $this->set_item('base_url', $base_url); } -- cgit v1.2.3-24-g4f1b From 93836ff351e3035bf368ed56da09ac27e354989f Mon Sep 17 00:00:00 2001 From: vlakoff Date: Mon, 31 Mar 2014 20:46:14 +0200 Subject: Different method for determining "base_url" Better performance by not using regex. --- system/core/Config.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'system/core/Config.php') diff --git a/system/core/Config.php b/system/core/Config.php index 728e28dd8..ec852b133 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -76,11 +76,16 @@ class CI_Config { // Set the base_url automatically if none was provided if (empty($this->config['base_url'])) { - $script_basename = basename($_SERVER['SCRIPT_NAME']); + $script_dir = strtr(dirname($_SERVER['SCRIPT_NAME']), '\\', '/'); + + if ($script_dir !== '/') + { + $script_dir .= '/'; + } $base_url = (is_https() ? 'https' : 'http') . '://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') - . preg_replace('/'.preg_quote($script_basename).'$/', '', $_SERVER['SCRIPT_NAME']); + . $script_dir; $this->set_item('base_url', $base_url); } -- cgit v1.2.3-24-g4f1b From 8bec33f37bd0e4542d5f963a446283c10a2b2ffe Mon Sep 17 00:00:00 2001 From: vlakoff Date: Tue, 1 Apr 2014 01:03:48 +0200 Subject: Remove spaces around concatenations per request --- system/core/Config.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system/core/Config.php') diff --git a/system/core/Config.php b/system/core/Config.php index ec852b133..b2689a6bb 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -83,9 +83,9 @@ class CI_Config { $script_dir .= '/'; } - $base_url = (is_https() ? 'https' : 'http') . '://' - . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') - . $script_dir; + $base_url = (is_https() ? 'https' : 'http').'://' + .(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') + .$script_dir; $this->set_item('base_url', $base_url); } -- cgit v1.2.3-24-g4f1b From b208f6502917b42a07b4bb7e5ffe2ef4b1359267 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Tue, 1 Apr 2014 01:21:51 +0200 Subject: Yet another method for determining "base_url" This one is great because we don't have to deal with the special cases: * in Windows, dirname('/foo/index.php') gives "/foo", but dirname('/index.php') gives "\" instead of "/" * dirname() doesn't include the trailing slash, with the expection of "/" (root) props @narfbg --- system/core/Config.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'system/core/Config.php') diff --git a/system/core/Config.php b/system/core/Config.php index b2689a6bb..41367d353 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -76,16 +76,9 @@ class CI_Config { // Set the base_url automatically if none was provided if (empty($this->config['base_url'])) { - $script_dir = strtr(dirname($_SERVER['SCRIPT_NAME']), '\\', '/'); - - if ($script_dir !== '/') - { - $script_dir .= '/'; - } - $base_url = (is_https() ? 'https' : 'http').'://' .(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') - .$script_dir; + .substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME']))); $this->set_item('base_url', $base_url); } -- cgit v1.2.3-24-g4f1b From 7ead65b5dc48c477dc3dfe3b969b1e14bb9a1b79 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Tue, 1 Apr 2014 02:09:25 +0200 Subject: Discard the unwanted changes --- system/core/Config.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'system/core/Config.php') diff --git a/system/core/Config.php b/system/core/Config.php index 41367d353..ad0e5f981 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -76,9 +76,16 @@ class CI_Config { // Set the base_url automatically if none was provided if (empty($this->config['base_url'])) { - $base_url = (is_https() ? 'https' : 'http').'://' - .(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') - .substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME']))); + if (isset($_SERVER['HTTP_HOST'])) + { + $base_url = (is_https() ? 'https' : 'http') + .'://'.$_SERVER['HTTP_HOST'] + .substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME']))); + } + else + { + $base_url = 'http://localhost/'; + } $this->set_item('base_url', $base_url); } -- cgit v1.2.3-24-g4f1b