How to get current page URL using PHP


In this tutorial, I will show you how to get current page URL using PHP. We will use $_SERVER variable to get the current page URL. $_SERVER is a super global variable, which is always available in all scopes. It is an array contains various information such as, headers, document path, script location, request method and so on.

How to get current page url using php

Get Current Page URL using PHP

By using the following simple PHP function you can get the current page URL with or without the query string as you needed.

function getCurrentPageURL($queryString = false) {

   $port = $_SERVER['SERVER_PORT'];
   $url  = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $port == 443) ? "https://" : "http://";
   $url .= $_SERVER['HTTP_HOST'];
   $url .= ($port == '80' || $port == '443' ? '' :  ':' . $port);
   $url .= $_SERVER['PHP_SELF'];
   $url .= ($queryString == true ? '?' . $_SERVER['QUERY_STRING'] : '');
            return $url;

echo "<b>Current page URL without query string :</b> " . getCurrentPageURL();
echo '<br>';
echo "<b>Current page URL with query string :</b> " . getCurrentPageURL(true);

If you want the query strings with the current page URL then call the getCurrentPageURL(true) function with true as parameter otherwise call the function with no parameter.

Complete Source Code – Get Current Page URL Using PHP

Download the complete source code from the below Download link. Also you can try the demo from the below Demo link. Please like and share this tutorial with others.

About Mitrajit

3 comments on “How to get current page URL using PHP

Leave a Reply

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

Prove you are a human *