This article mainly introduces how to use php to obtain the complete URL. First, let’s give a brief introduction to the novices about what a URL is. As explained on Baidu Encyclopedia, the Uniform Resource Locator is a concise representation of the location and access method of resources that can be obtained from the Internet. It is the address of a standard resource on the Internet. Every file on the Internet has a unique URL, which contains information indicating the location of the file and what the browser should do with it. In fact, it is simply a URL. That is, this article teaches you how to use php to obtain the complete URL and various parameters of the current page.
php The specific code examples for obtaining the url and other parameters are as follows:
1. Get the domain name or host address
echo $_SERVER['HTTP_HOST']."<br>"; #localhost
2. Get Web page address
echo $_SERVER['PHP_SELF']."<br>"; #/blog/testurl.php
3. Get the URL parameters
echo $_SERVER["QUERY_STRING"]."<br>"; #id=5
4. Get the complete url
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; #http://localhost/blog/testurl.php?id=5
5 , Get the complete url including the port number
echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; #http://localhost:80/blog/testurl.php?id=5
6. Get only the path
$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; echo dirname($url); #http://localhost/blog
Note: $_SERVER is a file containing header information (header), path (path), and script location. (script locations) and other information array. The items in this array are represented by Web server creation. There is no guarantee that every server will offer all items; servers may ignore some, or serve items not listed here.
【Recommended related articles】
What are the ways to request url in php
Detailed explanation of the definition of php parse_url() function and Usage
#Details introduction to one of the methods of obtaining URL parameters in JavaScript
The above is the detailed content of How to use PHP to get all the url parameter information of the current page?. For more information, please follow other related articles on the PHP Chinese website!