Skip to content

Instantly share code, notes, and snippets.

@kosinix
Created February 17, 2016 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosinix/fd0062ee341bcdf0e9a3 to your computer and use it in GitHub Desktop.
Save kosinix/fd0062ee341bcdf0e9a3 to your computer and use it in GitHub Desktop.
Compose URL from $_SERVER in PHP
<?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