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!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

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, WS, WSS, and whatever else.
  • Host – Better known as the “website address” to the non-technical folks.
  • Port – Usually left out. Commonly understood to be 80 for HTTP, 443 for HTTPS, and 21 for FTP.
  • Path – Beginners usually 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"] This is 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, and query.
https://site.com:443/path/file.php?p=123
$_SERVER["QUERY_STRING"] Query String only.
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

 

EXTRA) PARSE URL

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

For you guys who have a URL string from somewhere – You can use the parse_url() function to quickly get all the parts.

 

 

EXTRAS

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

 

HOW ABOUT THE HASH?

Want to get the hash section of the URL? For example, http://site.com/path/file.php#section. Sadly, it is nowhere to be found in $_SERVER. Your best bet is to use Javascript instead.

 

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!