How to Get a Current Page URL in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Convert Array to String in PHP?
- How to Convert String into Array in PHP?
- How to Extract All Email Addresses from String in PHP?
- PHP - Dropzone Add Download Button Example
- How to Convert String to Date in PHP?
- PHP JQuery Ajax Post Request Example
- How to Integrate Google Map using Gmaps JS?
- How to Get Current URL in Laravel?
- How to Get File Name without Extension in PHP?
- How to Get Current Route Name in Laravel?
- How to Get Product using Rakuten Marketing API in PHP?
- How to Remove Null Values from Array in PHP?