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

<?php
/**
 * 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://";
	}else{
		$url = "http://";
	}
	$url .= $_SERVER['SERVER_NAME'];
	if($_SERVER['SERVER_PORT'] != 80){
		$url .= ":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	}else{
		$url .= $_SERVER["REQUEST_URI"];	
	}
	return $url;
}

echo grabCurrentURL();
?>

 

0 Comments

Leave a Comment.