Home > php教程 > php手册 > PHP获取当前页面URL函数

PHP获取当前页面URL函数

WBOY
Release: 2016-05-25 16:45:26
Original
1322 people have browsed it

在php中获取页面URL文章我都介绍过了几十次了,但今天发现有一个非常不错的处理函数,下面给大家介绍一下。

在PHP中,没有默认的Function来获取目前所在页面的URL,所以今天就向大家介绍一个在PHP获取当前页面完整URL的PHP函数

代码

函数代码如下,调用时只需要使用 curPageURL() 就行啦!

<?php
/* 获得当前页面URL开始 */
function curPageURL() {
    $pageURL = &#39;http&#39;;
    if ($_SERVER["HTTPS"] == "on") {    // 如果是SSL加密则加上"s"
        $pageURL .= "s";
    }
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}
/* 获得当前页面URL结束 */
?>
Copy after login

补充一下server参数说明

<?php
//获取域名或主机地址 
echo $_SERVER[&#39;HTTP_HOST&#39;]."<br>"; #localhost
//获取网页地址 
echo $_SERVER[&#39;PHP_SELF&#39;]."<br>"; #/blog/testurl.php
//获取网址参数 
echo $_SERVER["QUERY_STRING"]."<br>"; #id=5
//获取用户代理 
echo $_SERVER[&#39;HTTP_REFERER&#39;]."<br>";
//获取完整的url
echo &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;REQUEST_URI&#39;];
echo &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;PHP_SELF&#39;].&#39;?&#39;.$_SERVER[&#39;QUERY_STRING&#39;];
#http://localhost/blog/testurl.php?id=5
//包含端口号的完整url
echo &#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;].&#39;:&#39;.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
#http://localhost:80/blog/testurl.php?id=5
//只取路径
$url=&#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;].$_SERVER["REQUEST_URI"]; 
echo dirname($url);
#http://localhost/blog
?>
Copy after login


本文链接:

收藏随意^^请保留教程地址.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template