How to get the current url in PHP

To get the current url in PHP you can use the following snippet.

Sample PHP

function GetCurrentUrl() {
	$currentUrl  = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http';
	$currentUrl .= '://' . $_SERVER['SERVER_NAME'];
	$currentUrl .= in_array( $_SERVER['SERVER_PORT'], array('443','80') ) ? '' : ':' . $_SERVER['SERVER_PORT'];
	$currentUrl .= $_SERVER['REQUEST_URI'];
	return $currentUrl;
}

Please Share us:
Please rate this snippet

One thought on “How to get the current url in PHP

Leave a Reply