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'];


To get the full URL, including the domain part:
echo get_current_url();
To get just the URL for internal use:
echo get_current_url(false);

This function will also work for HTTPS and non-standard ports.



Posted on 24.01.2011 14:04

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

Add your comment