How to get the full request URL in PHP?

How to get the full request URL of the page being processed in PHP?

asked Aug 11 by anonymous

1 Answer

If you are sure the request is a http or https one, and the PHP script is executed according to (e.g. by a load balancer or apache reverse proxy) the REQUEST_URI/HTTP_HOST which are set by the client, the PHP script can get the actual URL by

$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

If it is necessary, escape the $url such as:

$url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
answered Aug 12 by Eric Z Ma (43,790 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost