For PHP learners, you should all know that $_SERVER is an array containing information such as header, path, and script locations. Obviously, PHP mainly uses the $_SERVER system variable to obtain the complete URL address of the current page.
# Below we will introduce to you the implementation method of obtaining the complete URL in PHP through specific examples.
First we print $_SERVER directly, the code example is as follows:
<?php echo "<pre class="brush:php;toolbar:false">"; var_dump($_SERVER);
Output results, some screenshots are as follows:
The output result of $_SERVER is as shown in the figure, which is an array containing 41 elements.
So how do we get the current complete url? That is, how to determine whether the current page is HTTP or HTTPS, and how to obtain the domain name and path?
The solution code is as follows:
<?php $uri = $_SERVER['REQUEST_URI']; echo $uri;//输出:URI echo "<br>"; $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://": "http://"; $url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; echo $url;//输出完整的url
Here $uri is to obtain the path address of the file, that is, to obtain the value of the REQUEST_URI subscript. Then use the above method to determine whether the current URL header starts with HTTP or https. Finally, you can splice the three parts of the complete URL, including the header transfer protocol, domain name, and file address path.
The result is as shown below:
As shown in the figure, we successfully obtained the complete url address of the current page.
Note:
'REQUEST_URI', URI is used to specify the page to be accessed.
'HTTPS', is set to a non-empty value if the script is accessed via the HTTPS protocol.
'SERVER_PORT', The port used by the Web server. The default value is "80". If using SSL secure connection, this value is the HTTP port set by the user. (If $_SERVER['SERVER_PORT'] is equal to 443, it means that the url can be accessed directly. If it is equal to 8443, it means that the port number needs to be accessed.)
'HTTP_HOST', current request The contents of the Host: entry in the header, if present.
This article is an introduction to the method of obtaining the current complete url address in PHP. It is also very simple and easy to understand. I hope it will be helpful to friends in need
The above is the detailed content of How to get the current complete url address in php. For more information, please follow other related articles on the PHP Chinese website!