php教程 全面獲取url網址列參數多種方法
<?php
//獲取功能變數名稱或主機位址
echo $_SERVER['HTTP_HOST']." <br>";
//獲取網頁位址
echo $_SERVER['PHP_SELF']." <br>";
//獲取網址參數
echo $_SERVER["QUERY_STRING"]." <br>";
//來源網頁的詳細位址
echo $_SERVER['HTTP_REFERER']." <br>";
?>
方法二用? 號運算式,
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全域變數來實現了。