How to get the current page URL in PHP

Get current page URL in PHP

In the dynamic realm of development, understanding how to retrieve the current page URL in PHP is a fundamental skill. Whether you’re customizing themes, developing plugins, or implementing dynamic features, having the ability to access the current page URL opens up a world of possibilities. In this comprehensive guide, we will explore the solution for obtaining the current page URL in PHP, providing you with essential techniques and best practices to elevate your website’s functionality.

Introduction

You might need the current page URL in PHP whenever a page is opened in the browser. In this article, we will see how to get the current page URL in PHP using simple techniques.

Understanding the Significance of the Current Page URL in PHP

The current page URL is a crucial piece of information for various web development scenarios. It allows developers to create dynamic, context-aware functionalities, such as custom navigation, conditional content rendering, and analytics tracking. Knowing how to retrieve the current page URL is foundational for enhancing user experiences and implementing tailored solutions on your PHP site.

$_SERVER – Super Global Variable

PHP provides a built-in super global variable known as $_SERVER, which is used to get the server and environment execution information. We can also use this variable to get the current page URL.

With PHP we can get the current page URL in a single line of code. For example:

<?php
    $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    echo $url;
?>

Since a server can have both protocols (HTTP or HTTPS) and also the ports, the more efficient way of fetching the current page URL is:

<?php
	// function to get current page URL in PHP
	function getCurrentPageUrl() {
	    // default protocol
	    $pageURL = 'http';

	    // check if protocol is with SSL
	    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
	    	$pageURL .= "s";
	    }
	    
	    $pageURL .= "://";
	    
	    // check for port and create URL
	    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;
	}

	echo getCurrentPageUrl();
?>

This script will not only check the protocol but also if the URL contains a port.

Some more examples

We will cover more examples in the following section:

The HTTP_HOST is used to get the host or domain name. For example:

<?php  
    $domain = $_SERVER['HTTP_HOST'];  
    echo $domain;
?> 

Get the query string (the bit after the ? in a URL), that part is in this variable:

<?php
	$query_string = $_SERVER['QUERY_STRING'];
	echo $query_string;
?>

If you want just the parts of the URL after http://www.domain.com, the following will serve the purpose:

<?php 
	$url_except_domain = $_SERVER['REQUEST_URI']; 
	echo $url_except_domain;
?>

If you need the name of the current PHP page opened in the browser, the following code will be useful:

<?php  
    $current_page = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/") + 1);  
    echo $current_page;
?>  

In the next article, we will see how to get the current page URL in WordPress.

Conclusion

In conclusion, obtaining the current page URL in PHP is a fundamental skill that empowers developers to create dynamic and context-aware websites. Whether you opt for global variables, dedicated functions, or the direct use of the $_SERVER super global array, each method has its merits. Understanding the usage of these approaches allows you to choose the most suitable one for your specific development scenario.

By incorporating these techniques into your PHP projects, you gain the ability to create personalized and responsive functionalities, enhancing the overall user experience. As you navigate the ever-evolving landscape of web development, having a robust understanding of how to retrieve the current page URL in PHP is a valuable asset in your toolkit. Elevate your web development skills and bring a new level of dynamism to your websites with the knowledge acquired in this comprehensive guide.

Related Posts