Skip to content

Instantly share code, notes, and snippets.

@SleeplessByte
Last active December 10, 2015 22:18
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 SleeplessByte/4501468 to your computer and use it in GitHub Desktop.
Save SleeplessByte/4501468 to your computer and use it in GitHub Desktop.
Gets the current displayed url for a page. key <> value pairs can be stripped from the url by key.
<?php
/**
* Gets the current displayed url
* @strip array of key names to be stripped from the url
* @returns the url
*/
protected function get_current_url( $strip = NULL ) {
$url = isset( $_SERVER["HTTPS"] ) && $_SERVER['HTTPS'] == "on" ? 'https://' : 'http://';
$url .= ( strlen( $_SERVER["SERVER_NAME"] ) ? $_SERVER["SERVER_NAME"] : $_SERVER['HTTP_HOST']);
$url .= ( !in_array( strval( $_SERVER["SERVER_PORT"] ), array( "80", "443" ) ) ) ? $_SERVER["SERVER_PORT"] : '';
if (!is_null($strip)) :
$url .= preg_replace( "/[&\?](".implode("|", $strip).")=?[^&]*/", "", $_SERVER["REQUEST_URI"] );
else :
$url .= $_SERVER["REQUEST_URI"];
endif;
return $url;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment