Created
February 7, 2016 16:39
-
-
Save molayli/b9b6b05f2cdc3b1bb2d1 to your computer and use it in GitHub Desktop.
find the website base url dynamically
This file contains 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
/** | |
* @param bool $fullBase | |
* @return string | |
*/ | |
function baseUrl($fullBase=false){ | |
$scriptName = $_SERVER['SCRIPT_NAME']; | |
$parts = explode('/',$scriptName); | |
array_pop($parts); | |
$basePath = implode('/',$parts); | |
if($fullBase){ | |
$host = $_SERVER['HTTP_HOST']; | |
$host = rtrim($host,'/'); | |
$basePath = sprintf('%s://%s%s', | |
(isset($_SERVER['HTTPS'] )?'https':'http'), | |
$host, | |
$basePath | |
); | |
} | |
return $basePath; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment