php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78385 parse_url() does not include 'query' when question mark is the last char
Submitted: 2019-08-07 21:57 UTC Modified: -
From: benjamin dot morel at gmail dot com Assigned:
Status: Closed Package: *URL Functions
PHP Version: 7.3.8 OS: N/A
Private report: No CVE-ID: None
 [2019-08-07 21:57 UTC] benjamin dot morel at gmail dot com
Description:
------------
parse_url() returns the same result for these 2 distinct URLs:

http://example.com/foo
http://example.com/foo?

In the first URL, there is no query string, so the 'query' key is not set. This is OK. In the second URL, there is one, an empty one. As such, PHP should set the 'query' key in the result array to an empty string. At the moment, it's unset.

The issue with the current implementation is that libraries that parse and build URLs cannot differentiate these two, and will build a URL that's different from the parsed one.

Another issue is for libraries handling robots.txt files. According to Google (see https://developers.google.com/search/reference/robots_txt), the following rule:

Disallow: /*.php$

... will match '/filename.php', but not '/filename.php?'.

If I'm using parse_url() to extract the path and query string from the full URL, I will not be able to differentiate the two URLs above.

Test script:
---------------
var_export(parse_url('http://example.com/foo?'));

echo PHP_EOL, PHP_EOL;

var_export(parse_url('http://example.com/foo?', PHP_URL_QUERY));

Expected result:
----------------
array (
  'scheme' => 'http',
  'host' => 'example.com',
  'path' => '/foo',
  'query' => '',
)

''

Actual result:
--------------
array (
  'scheme' => 'http',
  'host' => 'example.com',
  'path' => '/foo',
)

NULL

Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-11 15:35 UTC] israfilov93 at gmail dot com
The following pull request has been associated:

Patch Name: ticket-78385 an empty string in parse_url() return when question markā€¦
On GitHub:  https://github.com/php/php-src/pull/5078
Patch:      https://github.com/php/php-src/pull/5078.patch
 [2020-01-13 09:18 UTC] nikic@php.net
Automatic comment on behalf of israfilov93@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f553e676ec5605f211a1539667be45fb4b518d2d
Log: Fixed #78385: Distinguish absent/empty query/fragment
 [2020-01-13 09:18 UTC] nikic@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC