Skip to content

Instantly share code, notes, and snippets.

@Magentron
Created October 16, 2017 14:34
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 Magentron/05ee3b8f62886878c2f1c3d76e8e3696 to your computer and use it in GitHub Desktop.
Save Magentron/05ee3b8f62886878c2f1c3d76e8e3696 to your computer and use it in GitHub Desktop.
<?php
/**
* @see: https://www.phpfreelancer.org/blog/php-get-current-url.php
*/
function get_current_url()
{
$url = false;
// check whether this script is being run as a web page
if (isset($_SERVER['SERVER_ADDR']))
{
$is_https = isset($_SERVER['HTTPS']) && 'on' == $_SERVER['HTTPS'];
$protocol = 'http' . ($is_https ? 's' : '');
$host = isset($_SERVER['HTTP_HOST'])
? $_SERVER['HTTP_HOST']
: $_SERVER['SERVER_ADDR'];
$port = $_SERVER['SERVER_PORT'];
$path_query = $_SERVER['REQUEST_URI'];
$url = sprintf('%s://%s%s%s',
$protocol,
$host,
$is_https
? (443 != $port ? ':' . $port : '')
: ( 80 != $port ? ':' . $port : ''),
$path_query
);
}
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment