How to Get a Current Page URL in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Hi Folks,

In this comprehensive tutorial, we will learn get current page url in php. you can see get full url in php. you will learn get absolute url in php. you can understand a concept of get current page full url in php.

I will give you following two simple examples to getting current url in php. so let's see the one by one example:

Example 1:

To get the current page URL in PHP, you can use the $_SERVER['REQUEST_URI'] superglobal variable. Here's an example:

$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

This code concatenates the protocol, domain, and request URI to form the complete URL of the current page.

Example 2:

You can also use the $_SERVER['PHP_SELF'] superglobal variable to get the path to the current PHP script, like this:

$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]";

However, this will not include any query parameters or fragment identifiers.

Note that the $_SERVER['REQUEST_URI'] and $_SERVER['PHP_SELF'] variables may not be secure and can be manipulated by attackers. You should sanitize and validate any input from these variables before using them in your code.

I hope it can help you...

Tags :