Created
July 6, 2015 14:10
-
-
Save guigmaster/d094a270155c094a2f18 to your computer and use it in GitHub Desktop.
Get full url include schema, port and optional request uri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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