Skip to content

Instantly share code, notes, and snippets.

@guigmaster
Created July 6, 2015 14:10
Show Gist options
  • Save guigmaster/d094a270155c094a2f18 to your computer and use it in GitHub Desktop.
Save guigmaster/d094a270155c094a2f18 to your computer and use it in GitHub Desktop.
Get full url include schema, port and optional request uri
<?php
function serverUrl($requestUri = null) {
switch (true) {
case (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] === true)):
case (isset($_SERVER['HTTP_SCHEME']) && ($_SERVER['HTTP_SCHEME'] == 'https')):
$scheme = 'https';
break;
default:
$scheme = 'http';
}
if (!empty($_SERVER['HTTP_HOST'])) {
$host = $_SERVER['HTTP_HOST'];
} else if (isset($_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'])) {
$name = $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
$host = ($port != 80) ? $name . ':' . $port : $name ;
}
$path = ($requestUri === true) ? $_SERVER['REQUEST_URI'] : '/' ;
return $scheme . '://' . $host . $path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment