Get the full URL in PHP

30 Nov, 2021
In this article, we will see how to get the complete URL of currently running pages using PHP, along with understanding their implementation through the examples. $_SERVER is a super global variable in PHP that contains the details related to the header, paths, and script locations. The status of HTTPS will be saved in the Global variable $_SERVER[‘HTTPS’]. So, using the $_SERVER[‘HTTPS’] in isset() function, that is used to check whether it exists or not. This will also tell us whether HTTPS is enabled or not. Check the value of $_SERVER[‘HTTPS’], if it is “on”, then HTTPS is enabled and we have to append “https” to the URL.
Approach: There are a few steps to get the complete URL of the currently running page which are given below:
Create a PHP variable that will store the URL in string format.
Check whether the HTTPS is enabled by the server. If it is, append “https” to the URL string. If HTTPS is not enabled, append “http” to the URL string.
Append the regular symbol, i.e. “://” to the URL.
Append the HTTP_HOST(The host to which we have requested, e.g. www.google.com, www.yourdomain.com, etc...) name of the server.
Append the REQUEST_URI(The resource which we have requested, e.g. /index.php, etc...) to the URL string.
Note: Use isset() function to check whether HTTPS is enabled or not. The isset() function is used to check a variable exists or not.Example 1: This example illustrates getting the url of the current page.
PHP
<?php    // Program to display URL of current page.    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')        $link = "https";    else $link = "http";          // Here append the common URL characters.    $link .= "://";          // Append the host(domain name, ip) to the URL.    $link .= $_SERVER['HTTP_HOST'];          // Append the requested resource location to the URL    $link .= $_SERVER['REQUEST_URI'];          // Print the link    echo $link;?>
Output:
https://ide.geeksforgeeks.org/
Example 2: Getting the url of the webpage using $_SERVER[‘HTTP_HOST’] that will return the host header from the current request.
PHP
<?php    // Program to display current page URL.    $link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']                 === 'on' ? "https" : "http") .                 "://" . $_SERVER['HTTP_HOST'] .                 $_SERVER['REQUEST_URI'];    echo $link;?>
Output:
https://ide.geeksforgeeks.org/
The output of the above code is https://ide.geeksforgeeks.org/ instead of https://ide.geeksforgeeks.org/index.php. In order to fix this problem, need to replace, $_SERVER[‘REQUEST_URI’] with $_SERVER[‘PHP_SELF’].
Program 3: This example is displaying the currently executing PHP file URL.
PHP
<?php    // Program to display complete URL  if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')       $link = "https";  else $link = "http";    // Here append the common URL  // characters.  $link .= "://";    // Append the host(domain name,  // ip) to the URL.  $link .= $_SERVER['HTTP_HOST'];    // Append the requested resource  // location to the URL  $link .= $_SERVER['PHP_SELF'];    // Display the link  echo $link;?>
Output:
https://ide.geeksforgeeks.org/index.php
Program 4: This example describes getting the complete url of the webpage.
PHP
<?php      // Program to display complete URL  $link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']              === 'on' ? "https" : "http") . "://" .               $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];    // Display the complete URL  echo $link;?>
Output:
https://ide.geeksforgeeks.org/index.php
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
bhaskargeeksforgeeks
PHP-Questions
Picked
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
PHP | Converting string to Date and DateTime
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?