How to get the full URL of the current page using PHP

How to get the full URL of the current page using PHP

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Understanding PHP URL Construction and Hash Fragments

When building web applications, you often need to reconstruct the full current URL for redirects, logging, or sharing links. A common stumbling block arises when dealing with URLs containing hash fragments (the part after the #). In your specific scenario, the request URI is www.mysite.com/category/subcategory/, but the browser's address bar displays www.mysite.com/category/subcategory/#myqueryhash.

Your current approach uses $_SERVER['HTTP_HOST'] combined with $_SERVER['REQUEST_URI']. While this correctly constructs the scheme, host, and path, it fails to capture the fragment identifier. This is not a bug in PHP; it is a fundamental limitation of how web servers handle HTTP requests versus what browsers display.

Why $REQUEST_URI Misses the Hash

The core issue lies in the HTTP protocol itself. According to RFC 3986, the hash component (fragment) is processed entirely by the user agent (the browser) before the request is sent to the server. When a page loads http://mysite.com/page#section, the browser parses the URL and discards everything after the # before sending the HTTP request packet to your web server.

Consequently, when PHP receives this request, $_SERVER['REQUEST_URI'] only contains //category/subcategory/. The hash fragment is physically absent from the HTTP request stream. No amount of server-side configuration or code logic can retrieve a value that was never transmitted to the server. This means your original snippet:

$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

Will inevitably return http://www.mysite.com/category/subcategory/ without the hash, regardless of what is visible in the browser.

Retrieving the Hash Fragment

To get the full URL including the fragment, you must bypass $_SERVER variables entirely and inspect the actual URL string currently loaded by the browser. In a vanilla PHP context, there is no superglobal that exposes this directly for security reasons (to prevent open redirects). However, you can use the $_SERVER['HTTP_HOST'], $_SERVER['SCRIPT_NAME'], or even better, the $_SERVER['REQUEST_URI'] combined with $_GET logic if the hash is passed via query parameters in a legacy system, but strictly for the fragment itself, you need to parse the raw URL from the browser context.

Since standard PHP superglobals strip the fragment, the most reliable way to reconstruct the exact string visible in the address bar requires accessing the request object or parsing the raw input stream if available, though this is rarely practical in standard web scripts. A more common pattern involves using the $_SERVER variables but acknowledging that the hash must be appended manually if you know it exists, or by utilizing a library that parses the full URL from the client context.

However, in modern development frameworks like Laravel, the framework handles this abstraction for you. If you are working within a Laravel application (or similar PHP frameworks), the request()->fullUrl() method will return the complete URL including query strings and fragments if they were present in the original request object sent by the browser.

If you are stuck with plain PHP and need to manually append a known fragment, or if you are dealing with a specific routing requirement where the hash is critical, you might consider using the parse_url function on a string that includes the fragment. But fundamentally, without client-side JavaScript to inject the fragment back into the server context, retrieving it purely via PHP server variables is impossible because the data does not exist in the server environment.

The Practical Solution

If your application logic requires the hash for tracking or deep linking, you cannot rely on $_SERVER alone. You generally need to capture the full URL on the client side and pass it back via a POST request or store it in session storage if the fragment is dynamic. Alternatively, if the fragment is static (like #myqueryhash), you can hardcode it or reconstruct it based on your application's state.

For most modern stacks, utilizing a framework like Laravel simplifies this significantly. The framework provides utilities that respect the full URL context:

use Illuminate\Support\Facades\Request;

// This returns the full URL including query strings and fragments if supported by the framework version
$fullUrl = Request::fullUrl(); 

Note that standard PHP does not expose the fragment in $_SERVER because the browser strips it. If you absolutely must have the fragment server-side, you likely need to handle it on the client side or use a specific proxy approach, but for standard URL reconstruction where the hash is known or derived from state, appending it explicitly after constructing the base URL is often the intended design pattern.

The takeaway is that $_SERVER['REQUEST_URI'] represents the path received by the server, not the final rendered address in the browser. Any solution claiming to magically retrieve the hash without client involvement misunderstands the HTTP protocol's separation of concerns regarding fragments.

Our network