Skip to content

Instantly share code, notes, and snippets.

@richjenks
Created June 13, 2016 11:58
Show Gist options
  • Save richjenks/d025f2bcc1caf21fb122206d05a503d9 to your computer and use it in GitHub Desktop.
Save richjenks/d025f2bcc1caf21fb122206d05a503d9 to your computer and use it in GitHub Desktop.
Attempt at providing the current URL in the cleanest possible manner
<?php
function url() {
$url = @( $_SERVER['HTTPS'] != 'on' ) ? 'http://' : 'https://'; // Protocol
$url .= $_SERVER['SERVER_NAME']; // Host
if ($_SERVER['SERVER_PORT'] != 80) $url .= ':'.$_SERVER['SERVER_PORT']; // Port
if ($_SERVER['REQUEST_URI'] !== '/') $url .= $_SERVER['REQUEST_URI']; // Request
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment