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, so 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 full page URL

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

Page URL With Arguments

 = $_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
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)
UK Electronic Designer
USEFUL?
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.

Comments

Your email address will not be published.