<?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 */