1 #测试网址: http://localhost/blog/testurl.php?id=5 2 3 //获取域名或主机地址 4 echo $_SERVER['HTTP_HOST']."<br>"; #localhost 5 6 //获取网页地址 7 echo $_SERVER['PHP_SELF']."<br>"; #/blog/testurl.php 8 9 //获取网址参数 10 echo $_SERVER["QUERY_STRING"]."<br>"; #id=5 11 12 //获取用户代理 13 echo $_SERVER['HTTP_REFERER']."<br>"; 14 15 //获取完整的url 16 echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 17 echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; 18 #http://localhost/blog/testurl.php?id=5 19 20 //包含端口号的完整url 21 echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 22 #http://localhost:80/blog/testurl.php?id=5 23 24 //只取路径 25 $url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; 26 echo dirname($url); 27 #http://localhost/blog