How to Get the Current Page URL in PHP?

When developing PHP based web application,  sometimes you need to grab the URL of current page.  Using the following PHP code snippet you will be able to get the URL of current webpage.

Useful Read: PHP Age Calculator

 * Grabs and returns the URL of current page.
 * @param   none
 * @return  URL of current page
function grabCurrentURL(){
	if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
		$url = "https://";
		$url = "http://";
	$url .= $_SERVER['SERVER_NAME'];
	if($_SERVER['SERVER_PORT'] != 80){
		$url .= $_SERVER["REQUEST_URI"];	
	return $url;

echo grabCurrentURL();



Leave a Comment.