Home > Backend Development > PHP Tutorial > How Can I Get the Full URL in PHP, Even with Masked URLs?

How Can I Get the Full URL in PHP, Even with Masked URLs?

Mary-Kate Olsen
Release: 2024-12-26 15:19:10
Original
891 people have browsed it

How Can I Get the Full URL in PHP, Even with Masked URLs?

Determining the Full URL in PHP

When dealing with masked URLs, the common approach of utilizing $_SERVER['HTTP_HOST'] and $_SERVER['PHP_SELF'] may not suffice in obtaining the complete URL presented in the browser's navigation bar. To resolve this issue and accurately retrieve the displayed URL, consider employing the following:

The variable $_SERVER['REQUEST_URI'] provides the full path of the requested URI, regardless of any .htaccess masking. To construct a valid URL, simply concatenate the following:

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

Note that the use of double quotes is crucial in defining the string correctly.

For websites supporting both HTTP and HTTPS connections, use the following:

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

Security Considerations

It's essential to emphasize the potential security risks associated with relying on $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'], as malicious actors may exploit these variables. Therefore, it's imperative to implement proper input validation and sanitization measures to mitigate these risks and ensure the integrity of your web application.

The above is the detailed content of How Can I Get the Full URL in PHP, Even with Masked URLs?. 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