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 "" $OurHttpHost will be ""

  //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 (with arguments)

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

Page URL (With Arguments)


Stripping Page URL

Getting URL without the arguments
$UrlWithoutArguments = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);    //Get page without any url arguments
//for "" $UrlWithoutArgumentswill 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 = "";
  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)
if (strpos($_SERVER["REQUEST_URI"], 'admin') !== False)
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.