Home > php教程 > php手册 > php获取当前页面完整url地址实例

php获取当前页面完整url地址实例

WBOY
Release: 2016-05-25 16:49:47
Original
2050 people have browsed it

在php中我们要获取 当前页面完整url地址需要使用到几个常用的php全局变量函数了,主要是以$_SERVER[]这些变量,下面我来给各位看一个获取当前页面完整url地址程序吧。

先来看一些PHP变量:

$_SERVER[ 'SERVER_NAME' ] #当前运行脚本所在服务器主机的名称。

$_SERVER[ 'QUERY_STRING' ] #查询(query)的字符串。   

$_SERVER[ 'HTTP_HOST' ] #当前请求的 Host: 头部的内容。   

$_SERVER[ 'HTTP_REFERER' ] #链接到当前页面的前一页面的 URL 地址。   

$_SERVER[ 'SERVER_PORT' ] #服务器所使用的端口   

$_SERVER[ 'REQUEST_URI' ] #访问此页面所需的 URI。  

有了些面函数我们就可以开始了,先来看一些base方法.

baseUrl的两种方法

方法一代码如下:

<?php
// baseUrl
function baseUrl(http: //pic3.phprm.com/2014/01/17/$uri=&#39;&#39;.jpg){
    $baseUrl = (isset($_SERVER[&#39;HTTPS&#39;]) && $_SERVER[&#39;HTTPS&#39;] != &#39;off&#39;) ? &#39;https://&#39; : &#39;http://&#39;;
    $baseUrl.= isset($_SERVER[&#39;HTTP_HOST&#39;]) ? $_SERVER[&#39;HTTP_HOST&#39;] : getenv(&#39;HTTP_HOST&#39;);
    $baseUrl.= isset($_SERVER[&#39;SCRIPT_NAME&#39;]) ? dirname($_SERVER[&#39;SCRIPT_NAME&#39;]) : dirname(getenv(&#39;SCRIPT_NAME&#39;));
    return $baseUrl . &#39;/&#39; . $uri;
}
?>
Copy after login

方法二代码如下:

<?php
/** 
 * Suppose, you are browsing in your localhost
 * http://localhost/myproject/index.php?id=8
 */
function baseUrl() {
    // output: /myproject/index.php
    $currentPath = $_SERVER[&#39;PHP_SELF&#39;];
    // output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index )
    $pathInfo = pathinfo($currentPath);
    // output: localhost
    $hostName = $_SERVER[&#39;HTTP_HOST&#39;];
    // output: http://
    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"], 0, 5)) == &#39;https://&#39; ? &#39;https://&#39; : &#39;http://&#39;;
    // return: http://localhost/myproject/
    return $protocol . $hostName . $pathInfo[&#39;dirname&#39;] . "/";
}
?>
Copy after login

方法三代码如下:

<?php
/** 
 *@author mckee
 *@blog  http://www.phprm.com
 */
function get_page_url() {
    $url = (isset($_SERVER[&#39;SERVER_PORT&#39;]) && $_SERVER[&#39;SERVER_PORT&#39;] == &#39;443&#39;) ? &#39;https://&#39; : &#39;http://&#39;;
    $url.= $_SERVER[&#39;HTTP_HOST&#39;];
    $url.= isset($_SERVER[&#39;REQUEST_URI&#39;]) ? $_SERVER[&#39;REQUEST_URI&#39;] : urlencode($_SERVER[&#39;PHP_SELF&#39;]) . &#39;?&#39; . urlencode($_SERVER[&#39;QUERY_STRING&#39;]);
    return $url;
}
echo get_page_url();
?>
Copy after login

下面说明一下获取当前页面完整路径的方法,代码如下:

<?php
function getFullUrl() {
    // 解决通用问题
    $requestUri = &#39;&#39;;
    if (isset($_SERVER[&#39;REQUEST_URI&#39;])) { //$_SERVER["REQUEST_URI"] 只有 apache 才支持,
        $requestUri = $_SERVER[&#39;REQUEST_URI&#39;];
    } else {
        if (isset($_SERVER[&#39;argv&#39;])) {
            $requestUri = $_SERVER[&#39;PHP_SELF&#39;] . &#39;?&#39; . $_SERVER[&#39;argv&#39;][0];
        } else if (isset($_SERVER[&#39;QUERY_STRING&#39;])) {
            $requestUri = $_SERVER[&#39;PHP_SELF&#39;] . &#39;?&#39; . $_SERVER[&#39;QUERY_STRING&#39;];
        }
    }
    //    echo $requestUri.&#39;<br />&#39;;
    $scheme = emptyempty($_SERVER["HTTPS"]) ? &#39;&#39; : ($_SERVER["HTTPS"] == "on") ? "s" : "";
    $protocol = strstr(strtolower($_SERVER["SERVER_PROTOCOL"]) , "/", true) . $scheme;
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":" . $_SERVER["SERVER_PORT"]);
    // 获取的完整url
    $_fullUrl = $protocol . "://" . $_SERVER[&#39;SERVER_NAME&#39;] . $port . $requestUri;
    return $_fullUrl;
}
?>
Copy after login

echo getFullUrl();注:由于php没有内置的函数,我们需要对url上的参数进行组合,从而实现整个url.

本文链接:

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

Related labels:
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