Home > Backend Development > PHP Tutorial > How Can I Get the Complete, Browser-Displayed URL in PHP, Accounting for .htaccess and Security?

How Can I Get the Complete, Browser-Displayed URL in PHP, Accounting for .htaccess and Security?

Linda Hamilton
Release: 2024-12-24 21:24:51
Original
979 people have browsed it

How Can I Get the Complete, Browser-Displayed URL in PHP, Accounting for .htaccess and Security?

Get the Complete URL in PHP

When attempting to retrieve the full URL using the code snippet $actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];, one faces a limitation due to .htaccess masking. The returned URL may not accurately reflect the URL displayed in the browser's navigation bar.

To obtain the exact URL as it appears in the browser, utilize the $_SERVER['REQUEST_URI'] variable. The following code illustrates its usage:

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

For flexibility across both HTTP and HTTPS protocols, employ the following code:

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

Security Considerations

It's important to note that this code has security implications. The client and server can modify HTTP_HOST and REQUEST_URI to arbitrary values. Adequate sanitization and input validation are crucial to prevent these values from being exploited in security contexts (CWE-20).

The above is the detailed content of How Can I Get the Complete, Browser-Displayed URL in PHP, Accounting for .htaccess and Security?. 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