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
Get URL without the domain name
$UrlPath = $_SERVER['REQUEST_URI'] ?? '/';
//For "https://example.com/my/page?x=1&y=2" $UrlPath will be "/my/page?x=1&y=2"
//For "http://example.com/ $UrlPath will be "/"
//For "https://example.com/path/only $UrlPath will be "/path/only"
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)
