PHP Get URL with Parameter

PHP Get URL with Parameter

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

Stefan Bogdanescu

Founder & Senior Architect

Decoding REQUEST_URI vs. PHP_SELF in PHP

When building a web application, you frequently need to extract the current path and query string from the incoming HTTP request. A common scenario involves distinguishing between the script's logical filename (e.g., somepage.php) and the actual URL segment requested by the client, including parameters like ?id=10. Developers often encounter confusion because built-in superglobals behave differently depending on whether you are inspecting the raw server data or the sanitized path information.

In the specific case of a request to www.someweb.com/somepage.php?id=10, standard approaches like accessing $_SERVER['REQUEST_URI'] or $_SERVER['PHP_SELF'] yield different results based on how the web server is configured and what stage of processing you are observing. If your goal is strictly to isolate the path and query string—specifically obtaining somepage.php?id=10—you must understand the distinction between the physical file path and the logical request URI.

Understanding the Superglobals

The variable $_SERVER['PHP_SELF'] returns the script's filename relative to the document root. In many configurations, especially when a web server like Apache handles requests via mod_rewrite or when files are aliased, this value often strips the query string entirely and may return just the base filename without the leading slash or domain context. This is why you might see somepage.php instead of the full path with parameters.

Conversely, $_SERVER['REQUEST_URI'] provides the raw URI requested by the client, including the query string. For the example URL provided, this variable typically returns /somepage.php?id=10. While this includes the leading slash, it is usually the most direct source for the "path + parameters" combination without needing to reconstruct the path manually. However, REQUEST_URI does not include the scheme (http/https) or the host domain; it represents exactly what follows the host in the URL.

If your application requires the full absolute URL including the domain (e.g., www.someweb.com/somepage.php?id=10), you must combine information from multiple sources. You would typically concatenate $_SERVER['HTTP_HOST'], the path from $_SERVER['REQUEST_URI'], and ensure the protocol is correctly set, often using $_SERVER['HTTPS'] to decide between https:// and http://.

Parsing Query Parameters Programmatically

While extracting the raw string is straightforward, working with the parameters inside that string requires parsing. Relying on simple string manipulation (like explode or substr) is fragile because it breaks down when parameters contain special characters, are empty, or appear multiple times. The robust approach is to leverage PHP's built-in parse_str() function or, preferably in modern contexts, the GET array from $_REQUEST or $_GET.

If you need the raw string representation of the path and parameters for logging, redirection, or URL rewriting logic, capturing $_SERVER['REQUEST_URI'] is the standard solution. If you are working within a framework like Laravel, the global request() helper abstracts this complexity. You can access the full URI via $request->fullUrl() or just the path via $request->fullUrlWithoutQuery().

Framework Considerations

If your project utilizes Laravel, the framework handles URL resolution and parsing internally. The url() helper function can generate URLs for you, but extracting the current request's path and query string is best done through the Request facade. For instance, $request->fullUrl() returns the complete URL including parameters. If you specifically need to separate the path from the query, you can use $request->path() combined with $request->getQueryString().

It is important to note that direct manipulation of server variables should be done cautiously. In production environments, trust boundaries are strict, and relying on raw $_SERVER data without validation can lead to security vulnerabilities if not sanitized properly. Always validate the input before using it to construct new URLs or redirect users, especially when dealing with external links or redirects that might expose sensitive information.

Best Practices for URL Handling

When constructing logic to extract specific parts of a URL, prioritize using framework-provided methods over raw server variable manipulation whenever possible. Frameworks like Laravel sanitize inputs and handle edge cases such as double slashes or encoded characters automatically. If you must work with raw PHP, ensure that $_SERVER['REQUEST_URI'] is treated as untrusted input until it has been parsed into an associative array using parse_str().

For high-performance applications, avoid repeatedly parsing the URI string if the data structure is already available in memory. Cache the parsed query parameters to reduce overhead on every request cycle. Finally, remember that the behavior of REQUEST_URI can vary slightly depending on the web server (Apache vs. Nginx) and whether a reverse proxy is involved, so always account for potential discrepancies in your production debugging strategy.

Our network