php 获取完整url地址_php技巧
程序员文章站
2022-04-21 17:45:09
...
主要是获取到地址栏的一些信息,域名,端口参数等
//获取域名或主机地址
echo $_SERVER['HTTP_HOST']."
";
//获取网页地址
echo $_SERVER['PHP_SELF']."
";
//获取网址参数
echo $_SERVER["QUERY_STRING"]."
";
//来源网页的详细地址
echo $_SERVER['HTTP_REFERER']."
";
?>
php 获得当前的脚本网址(只有路径)
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;
}
//实例调用方法
//echo GEtCurUrl();
php 获取url地址不包含路径(域名或ip地址)
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();
php 获取url地址包含端口路径
echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
复制代码 代码如下:
//获取域名或主机地址
echo $_SERVER['HTTP_HOST']."
";
//获取网页地址
echo $_SERVER['PHP_SELF']."
";
//获取网址参数
echo $_SERVER["QUERY_STRING"]."
";
//来源网页的详细地址
echo $_SERVER['HTTP_REFERER']."
";
?>
php 获得当前的脚本网址(只有路径)
复制代码 代码如下:
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;
}
//实例调用方法
//echo GEtCurUrl();
php 获取url地址不包含路径(域名或ip地址)
复制代码 代码如下:
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();
php 获取url地址包含端口路径
复制代码 代码如下:
echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];