Get full current page URL in PHP

Echo current page URL in a PHP page.

to echo or get the current page URL in a PHP page, use $_SERVER. Its build-in variable in PHP, which is used to fetch or echo the current page URL. It’s available for all scope and its super global variable.

Below examples show how it works and check URL on HTTPS or HTTP.

<?php
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'){

    $url = 'https://';
    }else{
       $url = "http://";
     }

$url .= $_SERVER['HTTP_HOST'];
$url .= $_SERVER['REQUEST_URI'];

echo $url; 
?>

The shortcode to get the current page URL is below.

If not using SSL Certificate/ using HTTP in URL .

$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

or

If  using SSL Certificate/ using HTTPS in URL .

$url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];