How to get Current URL in PHP

Full Code

Here's the full code that you can use:
  1. $protocol = (isset($_SERVER['HTTPS'])) ? "https://" : "http://";
  2. $URL = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

Definitions

Here's the definitions for the above code.
  • $protocol checks if the current path contains http or https protocol
  • $_SERVER['HTTP_HOST'] as per manual definition is the contents of the Host: header from the current request.
  • $_SERVER['REQUEST_URI'] gets the full path of the page.

Example

I am running on localhost server and I have the current path as http://localhost/howto/edit.php?id=4. Running the above code, will give me the ff values: $protocol = http:// $_SERVER['HTTP_HOST'] = localhost $_SERVER['REQUEST_URI'] = /howto/edit.php?id=4 $URL = http://localhost/howto/edit.php?id=4 That ends this tutorial. Happy Coding :)

Add new comment