How To Get Full URL & URL Parts In PHP – Simple Examples

Welcome to a quick tutorial on how to get the full URL and URL parts in PHP. Need to get the path, base, domain, or query string from the URL? In Javascript, we can pretty much get all this information with just one line of code. But sadly in PHP, things are a little… backward.

  • To get the full URL in PHP – $full = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  • To remove the query string from the full URL – $full = strtok($full, "?");

That should cover the basics, but if you need more specific “URL parts” – Read on for more examples!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes URL Basics PHP URL
Useful Bits & Links The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the example code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

URL BASICS

All right, let us start with some “boring” basic URL parts. Yep, this stuff is important if you are new.

 

THE VARIOUS URL PARTS

The common parts of a URL are:

  • Protocol – HTTP, HTTPS, FTP, or whatever else.
  • Host – Better known as the “website address” to the non-technical folks.
  • Port – Usually left out. But commonly understood to be 80 for HTTP, 443 for HTTPS, and 21 for FTP.
  • Path – Beginners commonly mistake this to be the “folder”, but it’s really not. I.E. The path can be virtual, not an actual physical folder.
  • File – Yes, the physical file name.
  • Query String – Extra information and parameters.

I know, it’s kind of ironic. The URL is supposed to be “easily understood” by humans, but there are so many parts to it.

 

 

GET FULL URL & URL PARTS

Now that you know the individual parts of a URL, let us now walk through how to obtain the full URL and the “common parts” using PHP.

 

1) URL PARTS IN PHP

1-url-parts.php
<?php
// (A) PROTOCOL
echo isset($_SERVER['HTTPS']) ? "https://" : "http://";

// (B) HOST
echo $_SERVER['HTTP_HOST'];

// (C) PORT
echo $_SERVER['SERVER_PORT'];

// (D) PATH + FILE + QUERY
echo $_SERVER['REQUEST_URI'];

// (E) QUERY
echo $_SERVER['QUERY_STRING'];
print_r($_GET);

All the URL parts can be obtained from the $_SERVER variable in PHP.

Variable URL Part & Example
$_SERVER['HTTPS'] Protocol (true if HTTPS is used)
https://site.com:443/path/file.php?p=123
$_SERVER['HTTP_HOST'] Host Name
https://site.com:443/path/file.php?p=123
$_SERVER['SERVER_PORT'] Port Number
https://site.com:443/path/path/file.php?p=123
$_SERVER['REQUEST_URI'] Path, File, Query
https://site.com:443/path/file.php?p=123
$_SERVER['QUERY_STRING'] Query String ($_GET is an array of the parsed query string)
https://site.com:443/path/file.php?p=123

 

 

2) GETTING THE FULL URL

2-full-url.php
<?php
// (A) GETFULLURL() : GETS THE FULL URL
//  $QUERY - INCLUDE QUERY STRING?
function getFullURL($query=false) {
  // (A1) THE PROTOCOL
  $url = (isset($_SERVER['HTTPS']) ? "https://" : "http://");

  // (A2) HOST
  $url .= $_SERVER['HTTP_HOST'];

  // (A3) ADD THE PORT ONLY IF IT IS NOT HTTP/HTTPS
  if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
    $url .= ":" . $_SERVER['SERVER_PORT'];
  }

  // (A4) THE PATH, FILE NAME, AND QUERY
  $url .= $_SERVER['REQUEST_URI'];

  // (A5) INCLUDE QUERY STRING?
  if ($query===false) { $url = strtok($url, '?'); }

  // (A6) THE FULL URL
  return $url;
}

// (B) GET CURRENT URL
echo getFullURL(true); // WITH QUERY
echo getFullURL(); // WITHOUT QUERY

This is pretty much the “expanded version” of the introduction snippet, packaged into a function for your convenience.

 

3) COMMON URL PARTS

As for the “rest of the parts” that are not included in $_SERVER, we will need to do some mix-and-match on our own. Here are a few of the common ones.

 

PROTOCOL & HOST

3-parts.php
// (A) PROTOCOL + DOMAIN
$host = isset($_SERVER['HTTPS']) ? "https://" : "http://"
      . $_SERVER['HTTP_HOST'] ;
echo $host;

https://site.com/path/file.php?p=123

 

 

PATH ONLY

3-parts.php
// (B) PATH ONLY
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
echo $path;

https://site.com/path/file.php?p=123

 

FILENAME ONLY

3-parts.php
// (C) FILENAME ONLY
// USE BASENAME() TO GET THE FILE + STRIP QUERY STRING
$file = basename($_SERVER['REQUEST_URI'], '?'. $_SERVER['QUERY_STRING']);
echo $file;

https://site.com/path/file.php?p=123

 

PATH WITH FILENAME

3-parts.php
// (D) PATH + FILENAME
$filepath = strtok($_SERVER['REQUEST_URI'], '?');
echo $filepath;

https://site.com/path/file.php?p=123

 

4) HASH

4-hash.php
<?php
$url = "http://site.com/path/file.php#section";
$hash = parse_url($url, PHP_URL_FRAGMENT);
echo $hash;

For you guys who are looking to get the hash from the current URL – You can’t. There’s literally no information of the hash in $_SERVER. But interestingly, we can parse a URL string to get the hash fragment…

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

Get URL Parts In PHP (click to enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *