How to Get Current Page URL in PHP

February 28, 2021
PHP
how to get current page url in php

Tutorial: How to Get Current Page URL in PHP with Source Code

About How to Get Current Page URL in PHP

Our Current url is https://www.campcodes.com/tutorials/php-tutorials/7631/how-to-get-current-page-url-in-php/

Source Code

$url=$_SERVER['REQUEST_URI'];
echo $url;

Output :

/how-to-get-current-page-url-in-php/

REQUEST_URI return only root website folder instead of returning full page.
if you want actual link then use this syntax

Source Code

$actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $actual_link ;
how to get current page url in php

how to get current page url in php

Output :
https://www.campcodes.com/tutorials/php-tutorials/7631/how-to-get-current-page-url-in-php/
In the above code $_SERVER[‘HTTP_HOST’] return the  domain name through which the current request is being fulfilled.

Leave a Reply

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