How to get the current url in PHP?

Wednesday, March 23, 2011 7:27
Posted in category PHP

Here is simple code to get the current url in PHP

<?php

	function selfURL() {
		if(!isset($_SERVER['REQUEST_URI'])) {
			$serverrequri = $_SERVER['PHP_SELF'];
		}
		else {
			$serverrequri = $_SERVER['REQUEST_URI'];
		}
		$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
		$protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos($_SERVER["SERVER_PROTOCOL"], '/')).$s;
		$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
		return $protocol."://".$_SERVER['SERVER_NAME'].$port.$serverrequri;
	}
?>

You can get the complete URL by using the below code.

 <?php echo selfURL(); ?> 

Found this little snippet that does almost the same…

You can leave a response, or trackback from your own site.

Leave a Reply

*