How to Get The Full URL and Parts In PHP

<?php
function getFullURL ($strip = false) {
// getFullURL() : gets the full URL
// PARAM $strip - strip away the query and hash portion
  // THE PROTOCOL
  $url = (isset($_SERVER['HTTPS']) ? "https://" : "http://");

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

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

  // THE PATH, FILE NAME, AND QUERY
  $url .= $_SERVER['REQUEST_URI'];
 
  // STRIP QUERY AND HASH
  if ($strip) {
    $url = strtok($url, '?');
    $url = strtok($url, '#');
  }
 
  // THE ENTIRE URL
  return $url;
}
?>
Credit: https://code-boxx.com/php-url-parts/

Comments

Popular posts from this blog

Useful aliases

Enable Search Engine Friendly (Pretty URLs) in Yii2