Skip to content

Instantly share code, notes, and snippets.

@plasticbrain
Created October 13, 2012 02:44
Show Gist options
  • Select an option

  • Save plasticbrain/3883001 to your computer and use it in GitHub Desktop.

Select an option

Save plasticbrain/3883001 to your computer and use it in GitHub Desktop.
PHP: Get the full URL of the current page
function current_page($params=NULL, $no_trailing_slash=TRUE) {
// http(s)://domain.com
$url = @$_SERVER['HTTPS'] == 'on' ? 'https://'. $_SERVER['SERVER_NAME'] : 'http://'.$_SERVER['SERVER_NAME'];
// add the port number, if there is one
if( @$_SERVER['SERVER_PORT'] != "80" && @$_SERVER['SERVER_PORT'] != "443" ) $url .= ':'. @$_SERVER['SERVER_PORT'];
// Append the query string
$url .= $_SERVER['REQUEST_URI'];
// [optionally] remove the trailing slash if there is one
if( $no_trailing_slash ) $url = rtrim($url, '/');
// Add any additional parameters that may have been included
if( $params ) $url .= $params;
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment