JS 가져오기:
<code>top<span>.location</span><span>.href</span> 顶级窗口的地址 this<span>.location</span><span>.href</span> 当前窗口的地址</code>
php 가져오기:
<code><span>#测试网址: http://localhost/blog/testurl.php?id=5</span><span>//获取域名或主机地址 </span><span>echo</span><span>$_SERVER</span>[<span>'HTTP_HOST'</span>].<span>"<br>"</span>; <span>#localhost</span><span>//获取网页地址 </span><span>echo</span><span>$_SERVER</span>[<span>'PHP_SELF'</span>].<span>"<br>"</span>; <span>#/blog/testurl.php</span><span>//获取网址参数 </span><span>echo</span><span>$_SERVER</span>[<span>"QUERY_STRING"</span>].<span>"<br>"</span>; <span>#id=5</span><span>//获取用户代理 </span><span>echo</span><span>$_SERVER</span>[<span>'HTTP_REFERER'</span>].<span>"<br>"</span>; <span>//获取完整的url</span><span>echo</span><span>'http://'</span>.<span>$_SERVER</span>[<span>'HTTP_HOST'</span>].<span>$_SERVER</span>[<span>'REQUEST_URI'</span>]; <span>echo</span><span>'http://'</span>.<span>$_SERVER</span>[<span>'HTTP_HOST'</span>].<span>$_SERVER</span>[<span>'PHP_SELF'</span>].<span>'?'</span>.<span>$_SERVER</span>[<span>'QUERY_STRING'</span>]; <span>#http://localhost/blog/testurl.php?id=5</span><span>//包含端口号的完整url</span><span>echo</span><span>'http://'</span>.<span>$_SERVER</span>[<span>'SERVER_NAME'</span>].<span>':'</span>.<span>$_SERVER</span>[<span>"SERVER_PORT"</span>].<span>$_SERVER</span>[<span>"REQUEST_URI"</span>]; <span>#http://localhost:80/blog/testurl.php?id=5</span><span>//只取路径</span><span>$url</span>=<span>'http://'</span>.<span>$_SERVER</span>[<span>'SERVER_NAME'</span>].<span>$_SERVER</span>[<span>"REQUEST_URI"</span>]; <span>echo</span> dirname(<span>$url</span>); <span>#http://localhost/blog</span></code>
위 내용은 URL 주소의 내용을 포함하여 URL 주소를 얻는 방법을 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.