Created
February 17, 2016 05:43
-
-
Save kosinix/fd0062ee341bcdf0e9a3 to your computer and use it in GitHub Desktop.
Compose URL from $_SERVER in PHP
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 compose_url(array $server){ | |
// Scheme | |
$scheme = ''; | |
if (isset($server['SERVER_PROTOCOL'])) { | |
$scheme = explode('/', $server['SERVER_PROTOCOL']); | |
$scheme = strtolower($scheme[0]); | |
if (isset($server['HTTPS']) && 'off' != $server['HTTPS']) { | |
$scheme .= 's'; | |
} | |
} | |
// Host | |
$host = isset($server['HTTP_HOST']) ? $server['HTTP_HOST'] : ''; | |
// Port | |
$port = ''; | |
if (isset($server['SERVER_PORT']) && '80' != $server['SERVER_PORT']) { | |
$port = ':'. (int) $server['SERVER_PORT']; | |
} | |
// URI | |
$uri = ''; | |
if (isset($server['REQUEST_URI'])) { | |
$uri = $server['REQUEST_URI']; | |
} else if (isset($server['PHP_SELF'])) { | |
$uri = $server['PHP_SELF']; | |
} | |
return $scheme.'://'.$host.$port.$uri; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment