Skip to content

Instantly share code, notes, and snippets.

@molayli
Created February 7, 2016 16:39
Show Gist options
  • Save molayli/b9b6b05f2cdc3b1bb2d1 to your computer and use it in GitHub Desktop.
Save molayli/b9b6b05f2cdc3b1bb2d1 to your computer and use it in GitHub Desktop.
find the website base url dynamically
/**
* @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