<?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全局變量來實現了。