Skip to content

@oknoorap /dynamic-base-url.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Return dynamic base URL of current file, on domain or within directory.
<?php
function base_url()
{
/* Get protocol information whether using secure or normal */
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"], 0, 5)) == 'https://' ? 'https://' : 'http://';
/* Call current script */
$path = $_SERVER['PHP_SELF'];
/*
* returns arrays:
* Array (
* [dirname] => /path/
* [basename] => script.php
* [extension] => php
* [filename] => script
* )
*/
$path_parts = pathinfo($path);
$directory = $path_parts['dirname'];
/*
* Replace backslash dirname,
* If it's in main directory only return / if within folder return dirname/
*/
$directory = str_replace('\\', '', $directory);
$directory = ($directory == "/") ? "/" : $directory .'/';
/* return domain name (localhost / domain.com) */
$host = $_SERVER['HTTP_HOST'];
/* Final Output */
return $protocol . $host . $directory;
}
echo base_url();
/* End of dynamic-base-url.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.