Home > php教程 > PHP源码 > body text

php 全面获取url地址栏参数多种方法

WBOY
Release: 2016-06-08 17:27:19
Original
1039 people have browsed it

我们这里总结了php 全面获取url地址栏参数多种方法哦,全部都用到了php自带的函数server

<script>ec(2);</script>
 代码如下 复制代码
//获取域名或主机地址
echo $_SERVER['HTTP_HOST']."
";
//获取网页地址
echo $_SERVER['PHP_SELF']."
";
//获取网址参数
echo $_SERVER["QUERY_STRING"]."
";
//来源网页的详细地址
echo $_SERVER['HTTP_REFERER']."
";
?>

方法二用?号表达式,

 代码如下 复制代码
echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

方法三也是一样

 代码如下 复制代码
function getServerName()
{
$ServerName = strtolower($_SERVER['SERVER_NAME']?$_SERVER['SERVER_NAME']:$_SERVER['HTTP_HOST']);
if( strpos($ServerName,'http://') )
{
return str_replace('http://','',$ServerName);
}
return $ServerName;
}
//实例调用方法
echo getServerName();

方法四,这个比较全面的获取url参数了,首先用REQUEST_URI如果服务器不支持就用PHP_SELF再不支持

 代码如下 复制代码
QUERY_STRING
function GetCurUrl()
{
if(!empty($_SERVER["REQUEST_URI"]))
{
$scrtName = $_SERVER["REQUEST_URI"];
$nowurl = $scrtName;
}
else
{
$scrtName = $_SERVER["PHP_SELF"];
if(empty($_SERVER["QUERY_STRING"]))
{
$nowurl = $scrtName;
}
else
{
$nowurl = $scrtName."?".$_SERVER["QUERY_STRING"];
}
}
return $nowurl;
}


上面这些实例,其实都用到了php $_SEVER全局变量来实现了。

Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template