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

      发布在:后端技术      评论:0 条评论
<pre class="brush:php;toolbar:false">javascript实现:<br/>top.location.href 顶级窗口的地址<br/>this.location.href 当前窗口的地址<br/>PHP实现<br/>#测试网址: http://localhost/blog/testurl.php?id=5<br/>//获取域名或主机地址 <br/>echo $_SERVER[&#39;HTTP_HOST&#39;].&quot;&lt;br&gt;&quot;; #localhost<br/>//获取网页地址 <br/>echo $_SERVER[&#39;PHP_SELF&#39;].&quot;&lt;br&gt;&quot;; #/blog/testurl.php<br/>//获取网址参数 <br/>echo $_SERVER[&quot;QUERY_STRING&quot;].&quot;&lt;br&gt;&quot;; #id=5<br/>//获取用户代理 <br/>echo $_SERVER[&#39;HTTP_REFERER&#39;].&quot;&lt;br&gt;&quot;; <br/>//获取完整的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<br/>//包含端口号的完整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<br/>//只取路径<br/>$url=&#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;].$_SERVER[&quot;REQUEST_URI&quot;]; <br/>echo dirname($url);<br/>#http://localhost/blog<br/><br/></pre><p>其中的http可以通过<code class="php variable" style="box-sizing: border-box; outline: 0px; border-radius: 4px; overflow-wrap: break-word; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);">$_SERVER</code><span style="color: rgb(51, 51, 51); font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun; background-color: rgb(255, 255, 255);">&nbsp;</span><code class="php plain" style="box-sizing: border-box; outline: 0px; border-radius: 4px; overflow-wrap: break-word; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);">[</code><span style="color: rgb(51, 51, 51); font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun; background-color: rgb(255, 255, 255);">&nbsp;</span><code class="php string" style="box-sizing: border-box; outline: 0px; border-radius: 4px; overflow-wrap: break-word; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);">&quot;REQUEST_SCHEME&quot;</code><span style="color: rgb(51, 51, 51); font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun; background-color: rgb(255, 255, 255);">&nbsp;</span><code class="php plain" style="box-sizing: border-box; outline: 0px; border-radius: 4px; overflow-wrap: break-word; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);">]获取</code></p><pre class="brush:php;toolbar:false">//获取当前url<br/>echo $_SERVER [ &quot;REQUEST_SCHEME&quot; ]. &#39;://&#39; . $_SERVER [ &quot;HTTP_HOST&quot; ]. $_SERVER [ &quot;REQUEST_URI&quot; ];</pre><p>参考来源:http://www.thinkphp.cn/topic/45176.html<code class="php plain" style="box-sizing: border-box; outline: 0px; border-radius: 4px; overflow-wrap: break-word; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"></code><br/></p>
相关文章
热门推荐