PHP中获取当前页面的完整URL

<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[&#39;HTTP_HOST&#39;].&quot;&lt;br&gt;&quot;; #localhost</p> <p>//获取网页地址<br /> echo $_SERVER[&#39;PHP_SELF&#39;].&quot;&lt;br&gt;&quot;; #/blog/testurl.php</p> <p>//获取网址参数<br /> echo $_SERVER[&quot;QUERY_STRING&quot;].&quot;&lt;br&gt;&quot;; #id=5</p> <p>//获取用户代理<br /> echo $_SERVER[&#39;HTTP_REFERER&#39;].&quot;&lt;br&gt;&quot;;</p> <p>//获取完整的url<br /> echo &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;REQUEST_URI&#39;];<br /> echo &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;PHP_SELF&#39;].&#39;?&#39;.$_SERVER[&#39;QUERY_STRING&#39;];<br /> #http://localhost/blog/testurl.php?id=5</p> <p>//包含端口号的完整url<br /> echo &#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;].&#39;:&#39;.$_SERVER[&quot;SERVER_PORT&quot;].$_SERVER[&quot;REQUEST_URI&quot;];<br /> #http://localhost:80/blog/testurl.php?id=5</p> <p>//只取路径<br /> $url=&#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;].$_SERVER[&quot;REQUEST_URI&quot;];<br /> echo dirname($url);<br /> #http://localhost/blog<br /> 复制代码</p>
RangeTime:0.006846s
RangeMem:205.11 KB
返回顶部 留言