How to Get Current Page URL, Domain, Query String in PHP

How to Get Current Page URL, Domain, Query String in PHP

In this tutorial, We would love to share with you, How to get current page URL in PHP, PHP get current URL path, PHP get full URL with parameters, PHP get a current page, PHP get current URL with query string, PHP get a domain from URL.

How to Get Current Page URL in PHP

Here you will discuss about superglobal variable $_SERVER, this is built-in PHP variable. We will use this variable get the current page URL in PHP. You can get every piece of information about the current URL using the $_SERVER superglobal.

Before we try to fetch the Current Page Full URL lets see how a normal URL structure looks like:

http://www.abc.com/dir1/test.php?glob=hello&name=world

Any typical URL like this can be broken into several common parts like:

  • HTTP: The URL protocol
  • www.abc.com – The domain name or the hostname.
  • dir1: The directory within the root
  • test.php – The actual PHP script
  • glob=hello – the first URL parameter (glob) and it’s a value (hello)
  • name=world – the second URL parameter (name) and its value (world)

In PHP, the $_SERVER superglobal variable allows easy methods to retrieve all this information about the current URI. The code is as follows:

PHP codeOutputRemark
echo $_SERVER[‘SERVER_PROTOCOL’];HTTP/1.1the url protocol
echo $_SERVER[‘HTTP_HOST’];www.abc.comthe domain name
echo $_SERVER[‘SCRIPT_NAME’];test.phpthe actual PHP script
echo $_SERVER[‘QUERY_STRING’];glob=hello&name=worldall the URL parameters

Now we will create a program that is used to get current page url.

PHP Program for get Current Page Url

<?php 

// PHP Program to show current page URL 
  
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
    $link = "https"; 
else
    $link = "http"; 
  
// Here append the common URL characters. 
$link .= "://"; 
  
// Append the host(domain name, ip) to the URL. 
$link .= $_SERVER['HTTP_HOST']; 
  
// Append the requested resource location to the URL 
$link .= $_SERVER['REQUEST_URI']; 
      
// Print the link 
echo $link; 
?> 

Program 1 full source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <title>PHP Program to show current page URL </title>
</head>
<body>
<h4>PHP Program to show current page URL </h4>


<?php 

// PHP Program to show current page URL 
  
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
    $link = "https"; 
else
    $link = "http"; 
  
// Here append the common URL characters. 
$link .= "://"; 
  
// Append the host(domain name, ip) to the URL. 
$link .= $_SERVER['HTTP_HOST']; 
  
// Append the requested resource location to the URL 
$link .= $_SERVER['REQUEST_URI']; 
      
// Print the link 
echo $link; 
?> 

</body>
</html>                                		

PHP Program show Current page full URL

<?php 
// PHP Program show Current page full URL	 
	
if(isset($_SERVER['HTTPS']) && 
			$_SERVER['HTTPS'] === 'on') 
	$uLink = "https"; 
	else
		$uLink = "http"; 

// Here append the common URL 
// characters. 
$uLink .= "://"; 
	
// Append the host(domain name, 
// ip) to the URL. 
$uLink .= $_SERVER['HTTP_HOST']; 
	
// Append the requested resource 
// location to the URL 
$uLink .= $_SERVER['PHP_SELF']; 
	
// Display the uLink 
echo $uLink; 
?> 

Program 2 full source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <title>PHP Program show Current page full URL </title>
</head>
<body>
<h4>PHP Program show Current page full URL </h4>

<?php 
// PHP Program show Current page full URL	 
	
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
	$uLink = "https"; 
	else
        $uLink = "http"; 

// Here append the common URL 
// characters. 
$uLink .= "://"; 
	
// Append the host(domain name, 
// ip) to the URL. 
$uLink .= $_SERVER['HTTP_HOST']; 
	
// Append the requested resource 
// location to the URL 
$uLink .= $_SERVER['PHP_SELF']; 
	
// Display the uLink 
echo $uLink; 
?> 


</body>
</html>                                		

PHP Program show Current full URL with Query String

<?php
	$uri = $_SERVER['REQUEST_URI'];
	echo $uri; // Outputs: URI
	 
	$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
	 
	$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
	echo $url; // Outputs: Full URL
	 
	$query = $_SERVER['QUERY_STRING'];
	echo $query; // Outputs: Query String
?>

Program 3 full source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <title>PHP Program show Current page full URL with Query String </title>
</head>
<body>
<h4>PHP Program show Current page full URL with Query String </h4>

<?php
	$uri = $_SERVER['REQUEST_URI'];
	echo $uri; // Outputs: URI
	 
	$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
	 
	$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
	echo $url; // Outputs: Full URL
	 
	$query = $_SERVER['QUERY_STRING'];
	echo $query; // Outputs: Query String
?>
</body>
</html>                                		

Other Important elements of $_SERVER that are very are useful and you must know that:

  • $_SERVER[‘SERVER_ADDR’]: IP address of the server
  • $_SERVER[‘REQUEST_METHOD’]: Returns the page access method used i.e. ‘GET’, ‘HEAD’, ‘POST’, ‘PUT’.
  • $_SERVER[‘REQUEST_TIME’]: timestamp of the start of the request.
  • $_SERVER[‘HTTP_REFERER’]: returns the referrer page uri – used in redirecting to last page after login
  • $_SERVER[‘SCRIPT_FILENAME’]: returns the path including the filename, like DIR
  • $_SERVER[‘HTTP_COOKIE’]. returns the raw value of the ‘Cookie’ header sent by the user agent.
  • $_SERVER[‘HTTP_ACCEPT_LANGUAGE’]): returns the default set language – useful for websites with multilingual content & readers
  • $_SERVER[‘HTTP_USER_AGENT’]: returns the kind of device being used to access (desktop/mobile device etc) – suitable for switching interface for different devices.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *