PHP获取路径

//获取域名或主机地址

echo $_SERVER['HTTP_HOST']."<br>"; #jiqing.nikon.com
echo 'http://'.$_SERVER['HTTP_HOST'].'/';  #http://jiqing.nikon.com/

//获取网页地址

echo $_SERVER['PHP_SELF']."<br>"; #/Home/picture

//获取网址参数

echo $_SERVER["QUERY_STRING"]."<br>"; #machine_type_id=2

//获取用户代理

echo $_SERVER['HTTP_REFERER']."<br>"; #http://jiqing.nikon.com/index.php/Home/index

//获取完整的url

echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."<br>"; # http://jiqing.nikon.com/index.php/Home/picture?machine_type_id=2

echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']."<br>"; #http://jiqing.nikon.com/Home/picture?machine_type_id=2

//包含端口号的完整url

echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]."<br>";#http://jiqing.nikon.com:80/index.php/Home/picture?machine_type_id=2

//只取路径

echo 'http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]."<br>"; # http://jiqing.nikon.com/index.php/Home

应用案例,根据浏览器类型,自动切换

vendor('Func.Func');
if (!Func::isMobile()) { // 跳转到手机端
    if ($_SERVER['QUERY_STRING']) {
        header("location: /index.php".$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
    } else {
        header("location: /index.php".$_SERVER['PHP_SELF']);
    }
}
posted @ 2018-09-06 11:54  TBHacker  阅读(5292)  评论(0编辑  收藏  举报