Get just the domain name

$_SERVER[‘HTTP_HOST’] gives you the domain name through which the current request is being fulfilled

  $OurHttpHost = $_SERVER['HTTP_HOST'];   //Returns just the domain name
  //for "https://mydomain.com/somepage" $OurHttpHost will be "mydomain.com"

  //If you need to ensure www. is not part of it (will be included if part of the request)
  $OurHttpHost = str_replace("www.", "", $OurHttpHost);

Get site URL

$SiteUrl = "https://" . $_SERVER['HTTP_HOST']
or ensuring there is no www:
$SiteUrl = "https://" . str_replace("www.", "", $_SERVER['HTTP_HOST']);

Get full page URL (domain name + page + any arguments)

$ThisPageUrl = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

Page URL (With Arguments)

$ThisPageUrl = $_SERVER["REQUEST_URI"];

Stripping Page URL

Getting URL without the arguments
$UrlWithoutArguments = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);    //Get page without any url arguments
 
//for "https://mydomain.com/somedirectory/somepage.php?auth=1234" $UrlWithoutArguments will be "/somedirectory/somepage.php"
Getting just the URL arguments

Will get everything after the ‘?’ (excluding the ‘?’)

$UrlArguments = parse_url($_SERVER["REQUEST_URI"], PHP_URL_QUERY);

Example usage:

  $UrlArguments = parse_url($_SERVER["REQUEST_URI"], PHP_URL_QUERY);

  $RedirectUrl = "https://mydomain.com";
  if (strlen($UrlArguments) > 0)
    $RedirectUrl .= '?' . $UrlArguments;

Does URI match?

Stripping any url arguments from it first
$ThisPageUrl = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);      //Get page without any url arguments
if($ThisPageUrl == '/my_page_name')
With any url arguments
if($_SERVER["REQUEST_URI"] == '/my_page_name?i=1')
Does URI contain?
if (strpos($_SERVER["REQUEST_URI"], 'admin') === False)
or
if (strpos($_SERVER["REQUEST_URI"], 'admin') !== False)
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

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