Last active
December 28, 2015 16:09
-
-
Save horsley/7527374 to your computer and use it in GitHub Desktop.
url合成的常用函数,获取基准url和部署子目录
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 | |
/** | |
* 获取本系统存放的目录 对应的url | |
* 当本系统部署在非站点根目录的时候 需要使用本函数获取系统根目录对应url | |
* 其后没有斜杠 | |
* @access public | |
* @return string | |
*/ | |
function get_baseurl() { | |
$baseURL = (!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https://" : "http://"; | |
if (!$host = $_SERVER['HTTP_HOST']) { | |
if (!$host = $_SERVER['SERVER_NAME']) { | |
$host = !empty($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : ''; | |
} | |
} | |
$baseURL .= $host. (preg_match('/(?:.*?):\d+/', $host) ? '': //如果host里面没有端口号,才考虑拼合 | |
($_SERVER["SERVER_PORT"] == "80" ? '' : ':'.$_SERVER["SERVER_PORT"])); | |
$baseURL .= get_basedir(); //去掉root目录和末尾的/index.php | |
return $baseURL; | |
} | |
/** | |
* 获取本系统存放的目录 | |
* 相对于站点根目录的相对目录 | |
* 只能是通过统一入口进入的调用 | |
* 返回的目录路径前面有杠,后面没杠 | |
* 如果部署在站点根目录,返回空文本 | |
* @access public | |
* @return string | |
*/ | |
function get_basedir() { | |
$base_dir = substr(dirname($_SERVER['SCRIPT_FILENAME']), strlen($_SERVER['DOCUMENT_ROOT'])); | |
//这里windows和linux环境可能存在差异导致丢失前面的斜杠,下面做兼容性处理 | |
if (!empty($base_dir) && $base_dir{0} != '/') { | |
$base_dir = '/'.$base_dir; | |
} | |
return $base_dir; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment