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!