Home > Daily Programming > PHP Knowledge > How to get the current complete url address in php

How to get the current complete url address in php

藏色散人
Release: 2018-12-12 09:35:46
Original
34039 people have browsed it

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.

How to get the current complete url address in php

# 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);
Copy after login

Output results, some screenshots are as follows:

How to get the current complete url address in php

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[&#39;REQUEST_URI&#39;];
echo $uri;//输出:URI 
echo "<br>";
$protocol = ((!empty($_SERVER[&#39;HTTPS&#39;]) && $_SERVER[&#39;HTTPS&#39;] != &#39;off&#39;) || $_SERVER[&#39;SERVER_PORT&#39;] == 443) ?
    "https://": "http://";
    
$url = $protocol . $_SERVER[&#39;HTTP_HOST&#39;] . $_SERVER[&#39;REQUEST_URI&#39;];
echo $url;//输出完整的url
Copy after login

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:

How to get the current complete url address in php

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template