Created
October 25, 2016 12:28
-
-
Save karim-khan/8359c9377a6a0cd046829c8188c48fb2 to your computer and use it in GitHub Desktop.
It is a base URL any web application base on php
This file contains hidden or 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
<?php | |
function base_url($atRoot = FALSE, $atCore = FALSE, $parse = FALSE) | |
{ | |
if (isset($_SERVER['HTTP_HOST'])) { | |
$http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http'; | |
$hostname = $_SERVER['HTTP_HOST']; | |
$dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); | |
$core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY); | |
$core = $core[0]; | |
$tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s"); | |
$end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir); | |
$base_url = sprintf($tmplt, $http, $hostname, $end); | |
} else $base_url = 'http://localhost/'; | |
if ($parse) { | |
$base_url = parse_url($base_url); | |
if (isset($base_url['path'])) if ($base_url['path'] == '/') $base_url['path'] = ''; | |
} | |
return $base_url; | |
} | |
echo base_url(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment