php

How to get the URL of the current page in PHP

In this tutorial, we are going to see how to get the URL of the current page in PHP. You can use the global variable $_SERVER to get the URL of the current page in PHP. $_SERVER is a super-global variable, which means that it is always available in all domains.

Similarly, if you want a complete URL of the current page, you need to check the name of the scheme (or protocol), either HTTP or HTTPS, as shown in the example below:
 

How to get the URL of the current page in PHP
<?php 
	if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
		$url = "https"; 
	else
		$url = "http"; 
	  
	// Add // to the URL.
	$url .= "://"; 
	  
	// Add the host (domain name, ip) to the URL.
	$url .= $_SERVER['HTTP_HOST']; 
	  
	// Add the requested resource to the URL
	$url .= $_SERVER['REQUEST_URI']; 
		  
	// Print the URL
	echo $url; 
?>

Output:

https://stackhowto.com/how-to-get-the-url-of-the-current-page-in-php/

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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