Skip to content

\Uri\WhatWg\Url lose host after calling withPath() or withQuery() #20501

Closed

Description

@asika32764

Description

The following code:

<?php

$url = new \Uri\WhatWg\Url('http://127.0.0.1:8080/path?query=123#fragment');

var_dump($url, $url->toAsciiString());

$url = $url->withPath('/hello-world');

var_dump($url, $url->toAsciiString());

Resulted in this output:

object(Uri\WhatWg\Url)#1 (8) {
  ["scheme"]=>
  string(4) "http"
  ["username"]=>
  NULL
  ["password"]=>
  NULL
  ["host"]=>
  string(9) "127.0.0.1"
  ["port"]=>
  int(8080)
  ["path"]=>
  string(5) "/path"
  ["query"]=>
  string(9) "query=123"
  ["fragment"]=>
  string(8) "fragment"
}
string(45) "http://127.0.0.1:8080/path?query=123#fragment"
object(Uri\WhatWg\Url)#2 (8) {
  ["scheme"]=>
  string(4) "http"
  ["username"]=>
  NULL
  ["password"]=>
  NULL
  ["host"]=>
  NULL
  ["port"]=>
  int(8080)
  ["path"]=>
  string(12) "/hello-world"
  ["query"]=>
  string(9) "query=123"
  ["fragment"]=>
  string(8) "fragment"
}
string(36) "http:/hello-world?query=123#fragment"

But I expected this output instead:

object(Uri\WhatWg\Url)#1 (8) {
  ["scheme"]=>
  string(4) "http"
  ["username"]=>
  NULL
  ["password"]=>
  NULL
  ["host"]=>
  string(9) "127.0.0.1"
  ["port"]=>
  int(8080)
  ["path"]=>
  string(5) "/path"
  ["query"]=>
  string(9) "query=123"
  ["fragment"]=>
  string(8) "fragment"
}
string(45) "http://127.0.0.1:8080/path?query=123#fragment"
object(Uri\WhatWg\Url)#2 (8) {
  ["scheme"]=>
  string(4) "http"
  ["username"]=>
  NULL
  ["password"]=>
  ["host"]=>
  string(9) "127.0.0.1"
  NULL
  ["port"]=>
  int(8080)
  ["path"]=>
  string(12) "/hello-world"
  ["query"]=>
  string(9) "query=123"
  ["fragment"]=>
  string(8) "fragment"
}
string(36) "http://127.0.0.1:8080/hello-world?query=123#fragment"

PHP Version

PHP 8.5.0RC4 (cli) (built: Nov  6 2025 03:02:00) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.5.0RC4, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.0RC4, Copyright (c), by Zend Technologies

Operating System

macOS

Activity

  1. changed the title [-]\Uri\WhatWg\Url miss host after calling withPath() or withQuery()[/-] [+]\Uri\WhatWg\Url lose host after calling withPath() or withQuery()[/+] on Nov 16, 2025
  2. TimWolla commented on Nov 16, 2025

    @TimWolla
    Member

    It appears to be an issue with IP addresses. Simpler:

    <?php
    
    var_dump((new \Uri\WhatWg\Url('http://example.com/path'))->withPath('/hello-world')->toAsciiString());
    var_dump((new \Uri\WhatWg\Url('http://127.0.0.1/path'))->withPath('/hello-world')->toAsciiString());
    var_dump((new \Uri\WhatWg\Url('http://[::1]/path'))->withPath('/hello-world')->toAsciiString());
    
  3. added a commit that references this issue on Nov 17, 2025
    8e6d375
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions