To obtain a url address in php, I will use the super global variable $_SERVER, which includes various parameter acquisition, such as HTTP_HOST, PHP_SELF, QUERY_STRING, etc., which will not be introduced here.
Introduction to several functions of PHP to obtain URL
The code is as follows
代码如下 |
复制代码 |
//获取域名或主机地址
echo $_SERVER['HTTP_HOST']." ";
//获取网页地址
echo $_SERVER['PHP_SELF']." ";
//获取网址参数
echo $_SERVER["QUERY_STRING"]." ";
//来源网页的详细地址
echo $_SERVER['HTTP_REFERER']." ";
?>
|
|
Copy code
|
代码如下 |
复制代码 |
// 说明:获取完整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;
}
?>
|
//Get domain name or host address
echo $_SERVER['HTTP_HOST']." ";
//Get the web address
echo $_SERVER['PHP_SELF']." ";
//Get URL parameters
echo $_SERVER["QUERY_STRING"]." ";
//Detailed address of the source web page
echo $_SERVER['HTTP_REFERER']." ";
?>
代码如下 |
复制代码 |
echo curPageURL();
?>
|
|
Combine the above functions to get the complete URL address
The code is as follows
|
Copy code
// Description: Get the complete 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;
}
?>
After defining this function, you can call it directly:
The code is as follows
|
Copy code
|
|
|
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