How to get URL of current page in PHP
Determining the full URL of the script currently executing is a fundamental task in web development, yet the method you choose depends entirely on what exactly you need: the raw request line, the clean path without query parameters, or the absolute URI including the host. The most direct answer to getting the parts after http://domain.example lies within PHP's built-in superglobals, specifically the $_SERVER array and the $_GET array.
The quickest way to retrieve the entire current URL as a string is by accessing the REQUEST_URI server variable. This property returns the path information and any query string present in the request line that was originally sent to the server. For example, if a user navigates to https://example.com/products?id=123&sort=asc, $_SERVER['REQUEST_URI'] will return /products?id=123&sort=asc. This includes the host and scheme only if they are relative; however, $_SERVER['HTTP_HOST'] combined with $_SERVER['SCRIPT_NAME'] can reconstruct the full absolute path if needed. It is important to note that REQUEST_URI reflects exactly what was typed into the browser or clicked, regardless of internal server rewrites. If your .htaccess rules or Nginx configuration have rewritten a URL like /old-page to /new-page, $_SERVER['REQUEST_URI'] will still show /old-page.
If your goal is strictly to isolate the path and query string while stripping the scheme (http/https) and the domain name, $_SERVER['REQUEST_URI'] is often the starting point. However, you must manually strip the leading slash if you want a clean string for concatenation or logging purposes. You might also encounter scenarios where $_GET is insufficient because it only contains parameters passed via the query string, not the path segments themselves. For instance, in a RESTful API endpoint like /api/v1/users, there are no query parameters, yet you still need to identify the current route context.
To handle these nuances robustly, especially when dealing with HTTPS or proxy headers, developers often look at $_SERVER['HTTPS'] and $_SERVER['HTTP_HOST']. If you are behind a reverse proxy or load balancer, the direct value of $_SERVER['HTTPS'] might be empty even if the connection is secure. In such environments, checking for the X-Forwarded-Proto header becomes essential to determine whether to prepend https:// or http://.
Laravel provides a cleaner abstraction over these raw server variables through its Request facade. If you are working within the Laravel ecosystem, using $request->fullUrl() or $request->url() offers a more predictable interface that respects your application's routing configuration and base path settings. This is particularly useful when generating links for emails or redirects, as it ensures the URL matches what the user actually sees in their browser, accounting for base paths defined in public/index.php.
A common pitfall arises when trying to construct URLs manually by concatenating strings from $_SERVER variables without normalizing them. This can lead to double slashes (//) or incorrect handling of query parameters if you attempt to merge parts of the URL yourself. Always prefer built-in utilities like parse_url() to dissect the URL into its components before reconstructing it, ensuring that the scheme, host, path, and query are handled correctly according to RFC standards.
When deciding between using raw $_SERVER variables or framework helpers, consider your environment's complexity. In a bare-metal setup with no rewrites, $_SERVER['REQUEST_URI'] is reliable and performant. In a complex environment with multiple proxy layers or base paths, leveraging the framework's request object prevents subtle bugs where the displayed URL differs from the internal logic path.
