PHP Get Address Bar URL Function_PHP Tutorial

WBOY
Release: 2016-07-13 10:44:15
Original
938 people have browsed it

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

Combine the above functions to get the complete URL address
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();
?>

The code is as follows Copy code

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
echo curPageURL();
?> http://www.bkjia.com/PHPjc/633120.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633120.htmlTechArticleTo obtain a url address in php, I will use the super global variable $_SERVER, which includes various parameter acquisitions. Such as HTTP_HOST, PHP_SELF, QUERY_STRING, etc., which will not be introduced here. PHP Get...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!