Yogesh Chauhan's Blog

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

in PHP on March 10, 2021

PHP has special variables called “super global variables” that are built-in variables and are available in all scopes anytime we want.

We are going to use PHP Superglobal $_SERVER to get that information.

$_SERVER variable holds information about paths, headers, script locations etc.

We can use the $_SERVER variable like this example:


Now let’s get the full URL in small pieces. The first thing we need to do is check if the URL is secure or not.

How to check if the URL is HTTP or HTTPS?

We can use $_SERVER[‘HTTPS’] to get that information from the super global $_SERVER.

Just like this:

    $http_info = 'https';
} else {
    $http_info = 'http';

How to get the host name?

The next thing is to get the host name. For that we can use $server[‘HTTP_HOST’] to get that info.

$host_name = $server['HTTP_HOST'];

Till this point we got the URL of the homepage of any website. But we need the complete URL so let’s get the third part.

We need the part after the domain name ends which is called URI or Uniform Resource Identifier. We can use $server[‘REQUEST_URI’] to get that part.

Just like this:

$file_path = $server['REQUEST_URI'];

Now, we all that we need to make the complete URL of any page. Let’s make a function out of all those parts so that we can use it anywhere on the website by simply calling it.

Function to get the full URL of current page in PHP

I’m using htmlentities function to convert characters to HTML entities which is similar to htmlspecialchars() function.

function get_the_current_url($server){
        $http_info = 'https';
    } else{
        $http_info = 'http';
    $host_name = $server['HTTP_HOST'];
    $file_path = $server['REQUEST_URI'];
    return $http_info . '://' . htmlentities($host_name) . '/' . htmlentities($file_path);

This is how you can call the function after placing it at appropriate location in the folder structure.

// store the URL in a variable
$current_url = get_the_current_url($_SERVER);
echo $current_url;

// or simply print it
echo get_the_current_url($_SERVER);

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
What is Git?MiscellaneousKubernetes vs. Docker? It’s a misleading phraseMiscellaneousWhat is a component helper function in React?ReactHTML canvas methodsHTMLWhat are Class Constants in PHP?PHPArray.from() Method in JavaScriptJavaScript