Home  >  Article  >  Backend Development  >  How to get url in php without parameters

How to get url in php without parameters

藏色散人
藏色散人Original
2022-01-14 09:17:203899browse

How to get url in php without parameters: 1. Get the domain name or host address through "$_SERVER['HTTP_HOST']"; 2. Get the web page address through "$_SERVER['PHP_SELF']" .

How to get url in php without parameters

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to get the url in php without parameters?

Test URL:

http://afish.cnblogs.com/p/3941211.html?id=1

The PHP code is implemented as follows:

//获取域名或主机地址
echo $_SERVER[&#39;HTTP_HOST&#39;]."<br />"; #afish.cnblogs.com
//获取网页地址
echo $_SERVER[&#39;PHP_SELF&#39;]."<br />"; #/p/3941211.html
//获取网址参数
echo $_SERVER["QUERY_STRING"]."<br />"; #id=1
//获取用户代理
echo $_SERVER[&#39;HTTP_REFERER&#39;]."<br />";
//获取完整的url
echo &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;REQUEST_URI&#39;];
echo &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;PHP_SELF&#39;].&#39;?&#39;.$_SERVER[&#39;QUERY_STRING&#39;];
#http://afish.cnblogs.com/p/3941211.html?id=1
//包含端口号的完整url
echo &#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;].&#39;:&#39;.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
#http://afish.cnblogs.com:80/p/3941211.html?id=1
//只取路径
$url=&#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;].$_SERVER["REQUEST_URI"];
echo dirname($url);
#http://afish.cnblogs.com/p

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to get url in php without parameters. For more information, please follow other related articles on 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