Created
January 8, 2014 11:00
-
-
Save Kaiyuan/8315085 to your computer and use it in GitHub Desktop.
格式化 URL,自动判断添加 http 或 https。
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 format_url($url,$slash) | |
{ | |
/* | |
* 规范化 URL | |
* 判断是否使用 HTTPS 链接,当是 HTTPS 访问时候自动添加 | |
* 自动添加链接前面的 http:// | |
* $slash 是判断是否要后面添加斜杠 | |
*/ | |
if (substr($url,0,4)!='http') { | |
@$if_https = $_SERVER['HTTPS']; //这样就不会有错误提示 | |
if ($if_https) { //如果是使用 https 访问的话就添加 https | |
$url='https://'.$url; | |
} else { | |
$url='http://'.$url; | |
} | |
} | |
if ($slash) { | |
$url = rtrim($url,'/').'/'; | |
} | |
return $url; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment