Home » Labs » Get the current url in PHP  

Get the current url in PHP


Filed under Labs

If you are a web developer then some times you need to get the current page URl. Here is a simple to get current url of a web page using php.

PHP function

<?php
// the following function will return current page url
function cPageURL()
{
	$pageURL = 'http';
 
	if ($_SERVER['HTTPS'] == 'on'){
		$pageURL .= 's';
	}
 
	$pageURL .= '://';
 
	if ($_SERVER['SERVER_PORT'] != '80'){
		$pageURL .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
	}
	else{
		$pageURL .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
	}
 
	return $pageURL;
}
?>

Call the function

You can now get the current page URL by calling the above function like following.

<?php
  echo cPageURL();
?>

Posts you may like: