php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78997 PHP parse_url incorrect
Submitted: 2019-12-19 09:51 UTC Modified: 2019-12-19 14:39 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: dinhbaouit at gmail dot com Assigned: geekcom (profile)
Status: Not a bug Package: *URL Functions
PHP Version: 7.2.26 OS: ubuntu
Private report: No CVE-ID: None
 [2019-12-19 09:51 UTC] dinhbaouit at gmail dot com
Description:
------------
In PHP <7.2.26 when using parse_url "host:port?query" the result return incorrect


Test script:
---------------
<?php

var_dump(parse_url("localhost:80?a=1"));

?>

Expected result:
----------------
php > var_dump(parse_url("localhost:80?a=1"));
array(3) {
  ["host"]=>
  string(9) "localhost"
  ["port"]=>
  string(2) "80"
  ["query"]=>
  string(3) "a=1"
}

Actual result:
--------------
php > var_dump(parse_url("localhost:80?a=1"));
array(3) {
  ["scheme"]=>
  string(9) "localhost"
  ["path"]=>
  string(2) "80"
  ["query"]=>
  string(3) "a=1"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-19 11:17 UTC] cmb@php.net
-Package: *General Issues +Package: *URL Functions
 [2019-12-19 11:17 UTC] cmb@php.net
I can confirm this behavior[1], but like the documentation[2]
states:

| Partial URLs are also accepted, parse_url() tries its best to
| parse them correctly.

So, in my opinion, this is not a bug (and might be duplicate
anyway).

[1] <https://3v4l.org/h4EMu>
[2] <https://www.php.net/parse_url>
 [2019-12-19 11:22 UTC] drtechno at mail dot com
The url must be formatted correctly for parse_url to work because it relies on the / symbol as a term separator.

$url = 'http://username:password@hostname:9090/path?arg=value#anchor';
$data=parse_url($url);
Print_r($data);
would return:
Array ( [scheme] => http [host] => hostname [port] => 9090 [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor ) 

$url = 'http://username:password@hostname:9090/path?arg=value#anchor';
var_dump(parse_url($url));

will output the string.length.count with the array:

array(8) { ["scheme"]=> string(4) "http" ["host"]=> string(8) "hostname" ["port"]=> int(9090) ["user"]=> string(8) "username" ["pass"]=> string(8) "password" ["path"]=> string(5) "/path" ["query"]=> string(9) "arg=value" ["fragment"]=> string(6) "anchor" }  

The "host" tag returns a FQDN only, local alias such as localhost will be listed in "scheme"
 [2019-12-19 14:18 UTC] geekcom@php.net
Hi folk, i did the same test with FQDN and...

Test script:
---------------

<?php

var_dump(parse_url('http://localhost:80?a=1'));

?>


Result:
----------------

array(4) {
  ["scheme"]=>
  string(4) "http"
  ["host"]=>
  string(9) "localhost"
  ["port"]=>
  int(80)
  ["query"]=>
  string(3) "a=1"
}

This result is expected .
 [2019-12-19 14:37 UTC] geekcom@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: geekcom
 [2019-12-19 14:37 UTC] geekcom@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2019-12-19 14:39 UTC] geekcom@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is not a bug according to the manual.

"This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly."
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Aug 03 08:01:25 2021 UTC