<p>PHP中获取当前页面的完整URL</p>
<p><br />
javascript实现:</p>
<p>top.location.href 顶级窗口的地址<br />
this.location.href 当前窗口的地址</p>
<p></p>
<p>复制代码<br />
PHP实现<br />
复制代码<br />
#测试网址: http://localhost/blog/testurl.php?id=5</p>
<p>//获取域名或主机地址<br />
echo $_SERVER['HTTP_HOST']."<br>"; #localhost</p>
<p>//获取网页地址<br />
echo $_SERVER['PHP_SELF']."<br>"; #/blog/testurl.php</p>
<p>//获取网址参数<br />
echo $_SERVER["QUERY_STRING"]."<br>"; #id=5</p>
<p>//获取用户代理<br />
echo $_SERVER['HTTP_REFERER']."<br>";</p>
<p>//获取完整的url<br />
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];<br />
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];<br />
#http://localhost/blog/testurl.php?id=5</p>
<p>//包含端口号的完整url<br />
echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];<br />
#http://localhost:80/blog/testurl.php?id=5</p>
<p>//只取路径<br />
$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];<br />
echo dirname($url);<br />
#http://localhost/blog<br />
复制代码</p>