Home > Backend Development > PHP Tutorial > How Can I Get the Full URL in PHP, Even with .htaccess Masks?

How Can I Get the Full URL in PHP, Even with .htaccess Masks?

Linda Hamilton
Release: 2024-12-21 00:02:08
Original
967 people have browsed it

How Can I Get the Full URL in PHP, Even with .htaccess Masks?

How to Obtain the Complete URL in PHP

To determine the full URL of a web page or application, PHP provides various mechanisms. One commonly employed method is to utilize a combination of $_SERVER['HTTP_HOST'] and $_SERVER['PHP_SELF'], as showcased in the code block mentioned in the original question.

The caveat with this approach arises when masks are implemented in the .htaccess file. These masks can potentially alter the URL displayed in the address bar, rendering it distinct from the actual path of the file on the server. To overcome this issue and accurately retrieve the URL that appears in the browser's navigation bar, consider leveraging the $_SERVER['REQUEST_URI'] variable.

$actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Copy after login

It's important to note that double-quoted strings are fully supported in PHP. To handle both HTTP and HTTPS protocols seamlessly, the following code snippet can be utilized:

$actual_link = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Copy after login

Security Considerations:

Be advised that sanitization and input validation measures are crucial when working with $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'] due to their potential for manipulation by malicious actors. It's essential to guard against arbitrary values being set for these variables, particularly in security-sensitive contexts.

The above is the detailed content of How Can I Get the Full URL in PHP, Even with .htaccess Masks?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template