Open In App

How to get the Current URL in PHP ?

In PHP, Getting the current URL is a common requirement for various tasks such as building dynamic navigation menus, tracking user activity, or generating links. PHP provides several superglobal arrays and functions to retrieve different parts of the URL, including the protocol, domain, path, query parameters, etc.

Approach

Syntax

// Get the current URL
$currentURL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $currentURL;

Important Points

Difference Between $_SERVER[‘REQUEST_URI’] and $_SERVER[‘HTTP_HOST’]

$_SERVER[‘REQUEST_URI’] $_SERVER[‘HTTP_HOST’]
Retrieves the URI of the current request, including the path and query string Retrieves the hostname portion of the current URL
Does not include the protocol or domain Does not include the path or query string

Features

Article Tags :