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
Mary-Kate OlsenOriginal
2024-12-26 15:19:10955browse

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]";

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]";

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!

Statement:
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