PHP get the current page full URL address instance _php tutorial

Source: Internet
Author: User
Tags urlencode
In PHP we want to get the current page full URL address need to use a few common PHP global variables function, mainly with $_server[] These variables, I will show you a get the current page full URL address program bar.

First look at some

$_server[' server_name '] #当前运行脚本所在服务器主机的名称.
A string of $_server[' query_string '] #查询 (QUERY).
$_server[' Http_host '] #当前请求的 HOST: The contents of the head.
$_server[' Http_referer '] #链接到当前页面的前一页面的 URL address.
$_server[' Server_port '] #服务器所使用的端口
$_server[' Request_uri '] #访问此页面所需的 URI.


With some polygon functions, we can start.

First look at some base methods


Two ways of BaseURL

Method One:

The code is as follows Copy Code

BaseUrl
function BaseUrl ($uri = ") {
$BASEURL = (isset ($_server[' https "]) && $_server[' https ']! = ' off ')? ' https://': '/HTTP ';
$baseUrl. = Isset ($_server[' http_host ')? $_server[' http_host ': getenv (' http_host ');
$baseUrl. = Isset ($_server[' script_name ')? DirName ($_server[' script_name '): DirName (getenv (' script_name '));
return $BASEURL. ' /'. $uri;
}


Method Two:

code as follows copy code

/**
* Suppose, browsing in your localhost
* Http://localhost/myproject/index.php?id=8
*/
function BaseUrl ()
{
Output:/myproject/index.php
$currentPath = $_server[' php_self ');

Output:array ([dirname] =/myproject [basename] = index.php [extension] = php [filename] + index)
$pathInfo = PathInfo ($currentPath);

Output:localhost
$hostName = $_server[' http_host ');

output:http://
$protocol = Strtolower (substr ($_server["Server_protocol"],0,5)) = = ' https://'? ' https://': '/HTTP ';

return:http://localhost/myproject/
Return $protocol. $hostName. $pathInfo [' dirname ']. " /";
}

Method Three

The code is as follows Copy Code

/**
* @author McKee
* @blog http://www.bKjia.c0m
*/
function Get_page_url () {
$url = (isset ($_server[' Server_port ')) && $_server[' server_port '] = = ' 443 ')? ' https://': '/HTTP ';
$url. = $_server[' http_host ');
$url. = Isset ($_server[' Request_uri ')? $_server[' Request_uri ': UrlEncode ($_server[' php_self ']). '?' . UrlEncode ($_server[' query_string ');
return $url;
}
Echo Get_page_url ();
?>

Here's how to get the full path to the current page:

code as follows copy code
< p>!--? php
function getfullurl () {
# fix common problem
$requestUri = ';
if (Isset ($_server[' Request_uri ']) { #$_server["Request_uri"] only Apache support,
$requestUri = $_server[' Request_uri ');
} else {
if (isset ($_server[' A RGV ']) {
$requestUri = $_server[' php_self ']. '? '. $_server[' argv '][0];
} else if (Isset ($_server[' query_string ') ]) {
$requestUri = $_server[' php_self ']. '? '. $_server[' query_string '];
}
}
//echo $requestUri. '
';
$scheme = Empty ($_server["HTTPS"])? ": ($_server[" HTTPS "] = =" on ")? "s": "";
$protocol = strstr (Strtolower ($_server["Server_protocol"]), "/", true). $scheme;
$port = ($_server["Server_port"] = = "80")? "": (":". $_server["Server_port"]);
# Gets the full URL
$_fullurl = $protocol. "://" . $_server[' server_name '. $port. $requestUri;
return $_fullurl;
}

Echo Getfullurl (); Note: PHP does not have built-in functions. We need to combine the parameters on the URL to implement the entire URL

http://www.bkjia.com/PHPjc/631252.html www.bkjia.com true http://www.bkjia.com/PHPjc/631252.html techarticle in PHP we want to get the current page full URL address need to use a few common PHP global variables function, mainly with $_server[] These variables, I will show you a get when ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.