Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 31, 2023 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/a556e61819e7d9cb543943fca7f0f9cd to your computer and use it in GitHub Desktop.
Save code-boxx/a556e61819e7d9cb543943fca7f0f9cd to your computer and use it in GitHub Desktop.
PHP Get Full & Partial URL

PHP GET FULL URL & URL PARTS

https://code-boxx.com/php-url-parts/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?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);
<?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
<?php
// (A) PROTOCOL + DOMAIN
$host = isset($_SERVER["HTTPS"]) ? "https://" : "http://"
. $_SERVER["HTTP_HOST"] ;
// echo $host;
// (B) PATH ONLY
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
// echo $path;
// (C) FILENAME ONLY
// USE BASENAME() TO GET THE FILE + STRIP QUERY STRING
$file = basename($_SERVER["REQUEST_URI"], "?". $_SERVER["QUERY_STRING"]);
// echo $file;
// (D) PATH + FILENAME
$filepath = strtok($_SERVER["REQUEST_URI"], "?");
// echo $filepath;
<?php
$url = "http://site.com/path/file.php#section";
$parts = parse_url($url);
print_r($parts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment