How to get full current page URL in PHP?

get full current page URL in PHP

0 563

You can simply use PHP predefined variable $_SERVER to get the full current page URL in PHP. The $_SERVER is a superglobal variable in PHP, $_SERVER is an array containing information such as headers, paths, and script locations.

In below script first determine server protocol(http or https) then hostname and URI, and merge all these values to get the perfect current page URL.

function getFullURL() {
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
 
$host = $_SERVER['HTTP_HOST'];
 
$uri = $_SERVER['REQUEST_URI'];
 
return $currentURL = $protocol.$host.$uri;
}

Add above function on your page and call it where you need to use current page url like this.

echo getFullURL();

 

 

Leave A Reply

Your email address will not be published.