Home >Backend Development >PHP Tutorial >php get the complete url address

php get the complete url address

高洛峰
高洛峰Original
2017-01-12 15:04:483513browse

Mainly get some information from the address bar, domain name, port parameters, etc.

<?php 
//获取域名或主机地址 
echo $_SERVER[&#39;HTTP_HOST&#39;]."<br>"; 
//获取网页地址 
echo $_SERVER[&#39;PHP_SELF&#39;]."<br>"; 
//获取网址参数 
echo $_SERVER["QUERY_STRING"]."<br>"; 
//来源网页的详细地址 
echo $_SERVER[&#39;HTTP_REFERER&#39;]."<br>"; 
?>

php Get the current script URL (only the path)

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 Get the url address without the path ( Domain name or IP address)

function getServerName() 
{ 
$ServerName = strtolower($_SERVER[&#39;SERVER_NAME&#39;]?$_SERVER[&#39;SERVER_NAME&#39;]:$_SERVER[&#39;HTTP_HOST&#39;]); 
if( strpos($ServerName,&#39;http://&#39;) ) 
{ 
return str_replace(&#39;http://&#39;,&#39;&#39;,$ServerName); 
} 
return $ServerName; 
} 
//实例调用方法 
echo getServerName();

php Get the url address including the port path

echo &#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;].&#39;:&#39;.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

For more php Get the complete url address related articles, please pay attention to the PHP Chinese website!

Statement:
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