Avatar ·

How to get the full URL of the current page in PHP?

Hello everyone! I'm a beginner PHP developer and I have a problem. I need to get the full URL of the current page, including the protocol (http or https), domain, path, and all GET parameters. I've tried several methods, but something always goes wrong. I tried using `$_SERVER['REQUEST_URI']` — it only returns the path, for example `/page.php?id=5`, without the domain and protocol. And `$_SERVER['HTTP_HOST']` only gives the domain, for example `example.com`. If I just concatenate them using `echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI"];`, when working over HTTPS the protocol will be incorrect — it will remain `http://`. I also noticed that `$_SERVER['HTTPS']` is sometimes undefined, especially on a local server. And if the page is opened with `www` or without `www` — that also affects it.