PHP Get Current URL

Get the current URL. Use it with social widgets that require the web page's URL

<?
function get_current_url($full = true)
{
	if (!$full) return $_SERVER['REQUEST_URI'];
	return 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'.$_SERVER['HTTP_HOST'].(($_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443) ? '' : ':'.$_SERVER['SERVER_PORT']).$_SERVER['REQUEST_URI'];
}
?>

Usage

To get the full URL, including the domain part: <pre> echo get_current_url(); </pre> To get just the URL for internal use: <pre> echo get_current_url(false); </pre> This function will also work for HTTPS and non-standard ports. S.


Comments

guest
Posted on 24.01.2011 13:04

far too simple. Request URI isn't always available. It's uncommon for it to be missing but does happen

Add your comment