How to get full URL of the current page in PHP
  John Mwaniki /   19 Sep 2021

How to get full URL of the current page in PHP

In this article, you will learn how to get the full URL of the current page using PHP.

But before doing so, let's first examine how a URL is structured.

Structure of a URL

Uniform Resource Locator (URL) is the address of a specific resource on a computer network such as a webpage or file.

It specifies the location of the resource on the network, and the mechanism of retrieving it.

The URL comprises 3 main parts which include the protocol, the domain name, and the file path as shown below:

The structure of a URL

The protocol indicates how the web browser should retrieve the information about a resource. The web standard protocol is http:// or https://(for secure connections). Other web protocols include ftp: (for handling file transfers) or mailto: (for opening the default mail client).

The domain name (also referred to as hostname) is the human-readable name of the specific location of a resource, which in most cases is a website.

On a website, there are files, and folders, which may also have subfolders. Resources such as web pages or different kinds of files can be placed on the domain root folder or within nested folders. To access these resources, you have to include the path to which the file is located and the name of the file in the URL.

Anything in a URL, that comes after the domain name is referred to as the path. A URL can also have parameters(for passing certain information), or anchors that allow visitors to navigate to a certain point within the resource.

Now that you understand the structure of a URL, let's proceed to the main topic.

 

 

Getting the URL in PHP

We will do this with the help of the PHP superglobal variable, $_SERVER.

$_SERVER is a built-in variable in PHP that is available in all scopes and holds an array of information about headers, paths, and script locations.

The table below comprises all the necessary $_SERVER elements that we will need in getting the page URL:

Element/Code Description
$_SERVER["HTTPS"] Contains information regarding if the script was queried using a secure(https) protocol.
$_SERVER["HTTP_HOST"] Contains the host/domain name from where the user is accessing the current page.
$_SERVER["PHP_SELF"] Contains the path and file name of the currently running script.
$_SERVER["REQUEST_URI"] Contains the URI of the currently running script.

How it works

We have a PHP file with the URL below. In it we will add code that should print out its full URL.

https://www.webdevsplanet.com/myfiles/myscript

Note: In our URL we have removed/hidden the file extension (.php).

From the URL structure section, we found out it has 3 main sections: Protocol, domain/hostname, and the path. We will find each at a time then concatenate them.

 

Getting the protocol

We use isset() function to check whether HTTPS is enabled or not. If it is, and the value of $_SERVER[‘HTTPS’] is “on”, we assign to $protocol variable "https://". Else, we assign it "http://".

<?php
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'){
 $protocol = "https://";
}
else{
 $protocol = "http://";
}
?>

Getting the domain name

To get the domain name, we get the value of $_SERVER["HTTP_HOST"] as shown below.

<?php
 $domain = $_SERVER["HTTP_HOST"];
 echo $domain;
?>

From our example URL, the output for this code will be: www.webdevsplanet.com

Getting the file path

Lastly, we get the file path, which follows the domain name.

There are two different ways of getting it.

i). Using $_SERVER["PHP_SELF"]. This returns the full filepath with the file extension eg. .php, .html, etc regardless of whether it has been removed/hidden with the .htaccess file.

<?php
 $path = $_SERVER["PHP_SELF"];
 echo $path;
?>

The output for this code will be: /myfiles/myscript.php

ii). Using $_SERVER["REQUEST_URI"]. Unlike PHP_SELF, this gives the exact URI as used in requests to access the file. If URL rewriting has been done to remove the file extension, then this will return it as such.

<?php
 $path = $_SERVER["REQUEST_URI"];
 echo $path;
?>

The output for this code will be: /myfiles/myscript

You can now get the full URL by concatenating the 3 parts from the examples above as shown below:

<?php
 $url = $protocol.$domain.$path;
?>

The output of the above will be:


https://www.webdevsplanet.com/myfiles/myscript
If you used $_SERVER["REQUEST_URI"]
or
https://www.webdevsplanet.com/myfiles/myscript.php
If you used $_SERVER["PHP_SELF"]
 

Putting it all together

Let's now combine everything above into one code.

<?php
 if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'){
   $url = "https://";   
  }
 else {
   $url = "http://";   
 }  
 $url.= $_SERVER['HTTP_HOST'];   // Appending the domain name
 $url.= $_SERVER['REQUEST_URI'];    // Appending the requested resource location
 
 echo $url; 
?>

Below is even a much shorter of doing it.

<?php
 $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https://" : "http://") .$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];  
 echo $url;
?>