php

Retrieve the Full URL in PHP

Two types of global variables can be used in PHP: the superglobal variable and the user-defined variable. $_SERVER is a superglobal array variable that is used to retrieve the full path of the current page. The protocol (HTTP or HTTPS) of the URL is also required to get the full URL of the page. If $_SERVER[‘HTTPS’] returns ‘on’, then HTTPS will be used with the URL address, otherwise, HTTP will be used. How the full URL address of the current page can be retrieved using the $_SERVER array has been explained in this tutorial.

Necessary Variables

The following superglobal variables are required to find out the URL address of the current page.

Superglobal Variables Purposes
$_SERVER[‘HTTPS’] It returns on if HTTPS protocol is used in the URL of the current page.
$_SERVER[‘HTTP_HOST’] It returns the name of the server of the current page.
$_SERVER[‘REQUEST_URI’] It returns the requested resource name.
$_SERVER[‘SERVER_PORT’] It returns the port number of the server.
$_SERVER[‘QUERY_STRING’] It returns the query string value if it exists in the URL address of the current page.

Example 1: Display the URL of the current page using a conditional statement

The following example shows how the conditional statement can be used to get the protocol used in the URL of the current page. Create a PHP file with the following script.

The value of $_SERVER[‘HTTP_HOST’] is used to get the domain name of the current URL. The value of $_SERVER[‘REQUEST_URI’] is used to get the name of the requested resource. isset() function is used to check if the $_SERVER[‘HTTPS’] is a set or not, and if set, then check the value of $_SERVER[‘HTTPS’] is it is on or not. Next, the values of these three variables are combined with ‘//:’ to retrieve the full URL of the current page.

<?php
// Read the domain name of the current page
$domain = $_SERVER['HTTP_HOST'];
//Read the requested resource
$resource = $_SERVER['REQUEST_URI'];
// Find out the protocol of the current url
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$protocol = "https";
else
$protocol = "http";

// Combine all parts to get the full URL address
$url = $protocol."://".$domain.$resource;
// Print the URL address of the current page
echo "<h3 style='color:blue'>The URL address of the current
page is : </h3>"
. $url;?>

Output:

The following output will appear after running the script from the server. The value of $_SERVER[‘HTTPS’] is not on for the local server. So the output shows the http protocol for the current URL.

Example 2: Display the URL of the current page using ternary operator

The following example shows the way to get the full URL of the current page using the ternary operator. Create a PHP file with the following script.

If the condition is used in the previous example to find out which protocol is used in the URL of the current page. The same task is done using the ternary operator in this script. isset() function is used to check if the $_SERVER[‘HTTPS’] is a set or not, and if the value of $_SERVER[‘HTTPS’] is on, then the ternary operator will return HTTPS, otherwise it will return HTTP. The other parts of the URL are retrieved and printed like the previous example.

<?php
// Find out the protocol of the current url
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']
== 'on' ? "https" : "http");
// Read the domain name of the current page
$domain =  $_SERVER['HTTP_HOST'];
//Read the requested resource
$resource = $_SERVER['REQUEST_URI'];
// Combine all parts to get the full URL address
$url = $protocol."://".$domain.$resource;
// Print the URL address of the current page
echo "<h3 style='color:red'>The URL address of the current page is : </h3>". $url;
?>

Output:

The following output will appear after running the script from the server. The value of $_SERVER[‘HTTPS’] is not on for the local server. So the output shows the HTTP protocol for the current URL.

Example 3: Display the URL of the current page based on port number

In the previous two examples, the value of $_SERVER[‘HTTPS’] is used to find out the protocol of the current URL of the page, and no query string is used with the URL. The following example shows how the $_SERVER[‘SERVER_PORT’] variable can be used to find out the protocol and retrieve the full URL address with the query string. Create a PHP file with the following script.

Here, multiple logical conditions and ternary operator is used to find out the protocol. If the value of $_SERVER[‘HTTPS’] is empty or set to off, then it will check the value of $_SERVER[‘SERVER_PORT’] to find out the protocol of the current URL. $_SERVER[‘QUERY_STRING’] variable is used to retrieve the query string value from the URL.

<?php
// Find out the protocol of the current url
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']
!= 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
// Read the domain name of the current page
$domain =  $_SERVER['HTTP_HOST'];
// Read the requested resource
$resource = $_SERVER['REQUEST_URI'];
// Read the query string value
$query = $_SERVER['QUERY_STRING'];
// Combine all parts to get the full URL address
$url = $protocol.$domain.$resource;
// Print the URL address of the current page
echo "<h3 style='color:green'>The full URL address of the current page is : </h3>". $url;
// Print the query string part
echo "<h4 style='color:purple'>The query string is : </h4>". $query;
?>

Output:

The following output will appear after executing the above script from the server without any query string. No query string is provided in the URL. So the output shows the empty query string.

The following output will appear after executing the above script from the server with the query string. The following output shows the URL address with the query string and the query string separately.

Conclusion

Different ways of retrieving the full URL of the current page are shown in this tutorial by using various examples. The way to separate the URL and the query string is also shown in this tutorial. Hopefully, this tutorial will help the readers know the way of reading the full URL of the current page using the PHP script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.