#測試網址: http://localhost/blog/testurl.php?id=5
//取得網域名稱或主機位址
echo $_SERVER['HTTP_HOST']."
"; #localhost
//取得網頁位址
echo $ _SERVER['PHP_SELF']."
"; #/blog/testurl.php
//取得網址參數
echo $_SERVER["QUERY_STRING"]."
"; # id=5
//取得使用者代理
echo $_SERVER['HTTP_REFERER']."
";
//取得完整的url
echo 'http ://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'? '.$_SERVER['QUERY_STRING'];
#http://localhost/blog/testurl.php?id=5
//包含連接埠號碼的完整url
echo 'http: //'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
#http://localhost:80/blog/testurl.php? id=5
//只取路徑
$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
echo dirname($ url);
#http://localhost/blog
javascript實現:
top.location.href 頂層視窗的位址
this.location.href 目前視窗的位址