In php, if we want to get the complete url address of the current page, we need to use several common php global variable functions, mainly using the variables $_SERVER[]. Let me show you how to get the complete url address of the current page. Program it.
Let’s take a look at some first
$_SERVER[ 'SERVER_NAME' ] #The name of the server host where the script is currently running.
$_SERVER[ 'QUERY_STRING' ] #Query string.
$_SERVER[ 'HTTP_HOST' ] #Contents of the Host: header of the current request.
$_SERVER[ 'HTTP_REFERER' ] #The URL address of the previous page linked to the current page.
$_SERVER[ 'SERVER_PORT' ] #Port used by the server
$_SERVER[ 'REQUEST_URI' ] #The URI required to access this page.
With some surface functions we can start
Let’s first look at some base methods
Two methods of baseUrl
Method 1:
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;
}
|
// baseUrl
function baseUrl($uri=''){
$baseUrl = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https://' : 'http://';
$baseUrl .= isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : getenv('HTTP_HOST');
代码如下 |
复制代码 |
/**
* Suppose, you are 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']."/";
}
|
$baseUrl .= isset($_SERVER['SCRIPT_NAME']) ? dirname($_SERVER['SCRIPT_NAME']) : dirname(getenv('SCRIPT_NAME'));
return $baseUrl.'/'.$uri;
}
代码如下 |
复制代码 |
/**
*@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();
?>
|
|
Method 2:
The code is as follows |
Copy code |
/**
* Suppose, you are 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 3
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();<🎜>
?>
|
The following explains how to get the full path of the current page:
The code is as follows
代码如下 |
复制代码 |
function getFullUrl(){
# 解决通用问题
$requestUri = '';
if (isset($_SERVER['REQUEST_URI'])) { #$_SERVER["REQUEST_URI"] 只有 apache 才支持,
$requestUri = $_SERVER['REQUEST_URI'];
} else {
if (isset($_SERVER['argv'])) {
$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"]);
# 获取的完整url
$_fullUrl = $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $requestUri;
return $_fullUrl;
}
|
|
Copy code
|
|
function getFullUrl(){
# Solve common problems
$requestUri = '';
If (isset($_SERVER['REQUEST_URI'])) { #$_SERVER["REQUEST_URI"] is only supported by apache,
$requestUri = $_SERVER['REQUEST_URI'];
} else {
If (isset($_SERVER['argv'])) {
$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"]);
# Get the complete url
$_fullUrl = $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $requestUri;
Return $_fullUrl;
}
echo getFullUrl(); Note: Since PHP does not have a built-in function, we need to combine the parameters on the url to achieve the entire url
http://www.bkjia.com/PHPjc/631252.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631252.htmlIn php, we need to use several common php global variable functions to get the complete url address of the current page. Mainly based on the variables $_SERVER[], let me show you how to get it...