1 来自:http://www.cnblogs.com/A-Song/archive/2011/12/14/2288215.html 2 3 #测试网址: http://localhost/blog/testurl.php?id=5 4 5 //获取域名或主机地址 6 echo $_SERVER['HTTP_HOST']."<br>"; #localhost 7 8 //获取网页地址 9 echo $_SERVER['PHP_SELF']."<br>"; #/blog/testurl.php 10 11 //获取网址参数 12 echo $_SERVER["QUERY_STRING"]."<br>"; #id=5 13 14 //获取用户代理 15 echo $_SERVER['HTTP_REFERER']."<br>"; 16 17 //获取完整的url 18 echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 19 echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; 20 #http://localhost/blog/testurl.php?id=5 21 22 //包含端口号的完整url 23 echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 24 #http://localhost:80/blog/testurl.php?id=5 25 26 //只取路径 27 $url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; 28 echo dirname($url); 29 #http://localhost/blog 30 31 32 33 34 35 以下内容由本人添加: 36 37 //取得当前url目录 38 39 $url = 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 40 echo $url = substr($url, 0, strlen($url) - strlen(basename(__FILE__)));