PHP: How to Get the Current Page URL

This can be done by following steps:

  • Create a PHP variable which will store the URL in string format.
  • Check whether the HTTPS is enabled by the server .If it is, append “https” to the URL string. If HTTPS is not enabled, append “http” to the URL string.( By Using isset() function to check whether HTTPS is enable or not. )
  • Append the regular symbol, i.e. “://” to the URL.
  • Append the HTTP_HOST(The host to which we have requested, e.g. www.google.com, www.yourdomain.com, etc…) name of the server.
  • Append the REQUEST_URI(The resource which we have requested, e.g. /index.php, etc…) to the URL string.
<?php
// Program to display complete URL

if(isset($_SERVER['HTTPS']) &&
$_SERVER['HTTPS'] === 'on')
$link = "https";
else
$link = "http";

// Here append the common URL
// characters.
$link .= "://";

// Append the host(domain name,
// ip) to the URL.
$link .= $_SERVER['HTTP_HOST'];

// Append the requested resource
// location to the URL
$link .= $_SERVER['PHP_SELF'];

// Display the link
echo $link;
?>

PHP: How to Get the Current Page URL
You may Also Like
Scroll to top