We have many functions that we can use to get the url address in php. The main thing is that the global variable server provides me with many methods. Let’s take a look.
Common functions
The code is as follows
代码如下 |
复制代码 |
//获取域名或主机地址
echo $_SERVER['HTTP_HOST']." "; #localhost
//获取网页地址
echo $_SERVER['PHP_SELF']." "; #/blog/testurl.php
//获取网址参数
echo $_SERVER["QUERY_STRING"]." "; #id=5
//获取用户代理
echo $_SERVER['HTTP_REFERER']." ";
//获取完整的url
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
#http://localhost/blog/testurl.php?id=5
//包含端口号的完整url
echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
#http://www.bKjia.c0m /blog/testurl.php?id=5
//只取路径
$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
echo dirname($url);
#http://localhost/blog
|
|
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']." "; #localhost |
//Get the web address
echo $_SERVER['PHP_SELF']."
"; #/blog/testurl.php
//Get URL parameters
echo $_SERVER["QUERY_STRING"]."
"; #id=5
//Get user agent
echo $_SERVER['HTTP_REFERER']."
";
//Get the complete url
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
#http://localhost/blog/testurl.php?id=5
//Full url including port number
echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
#http://www.bKjia.c0m /blog/testurl.php?id=5
//Get only the path
$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
echo dirname($url);
#http://localhost/blog
Look at an example to get the complete url
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;
}
?>
http://www.bkjia.com/PHPjc/632634.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632634.htmlTechArticleWe have many functions to use to get the url address in php, mainly the global variable server provides me with a lot Method, let’s take a look below. Commonly used function codes are as follows. Copy...
|