YogeshChauhan . com

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

The 7 Security Objectives of Any Organization for IT and Network SecurityMiscHow to Secure Web or Mobile Browsers?MiscWordPress: How to get field values in Advanced Custom Fields?WordPressWhat are keys in React?Reactinclude, include_once, require, require_once in PHPPHPWhat is ECMAScript?JavaScript