Home >php教程 >php手册 >PHP获取地址栏URL函数

PHP获取地址栏URL函数

WBOY
WBOYOriginal
2016-06-13 09:47:30907browse

在php获取一个url地址我会使用到超级全局变量$_SERVER,他包括了各种参数获取,如HTTP_HOST、PHP_SELF、QUERY_STRING等等,这里就不介绍了。

PHP获取URL的几个函数介绍

 代码如下 复制代码

//获取域名或主机地址
echo $_SERVER['HTTP_HOST']."
";
//获取网页地址
echo $_SERVER['PHP_SELF']."
";
//获取网址参数
echo $_SERVER["QUERY_STRING"]."
";
//来源网页的详细地址
echo $_SERVER['HTTP_REFERER']."
";
?>

把上面的几个函数组合一下,即可得到完整的URL地址

 代码如下 复制代码

// 说明:获取完整URL

function curPageURL()
{
    $pageURL = 'http';

    if ($_SERVER["HTTPS"] == "on")
    {
        $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;
}
?>

定义该函数之后就可以直接调用了:

 代码如下 复制代码

  echo curPageURL();
?>

Statement:
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