International PHP Conference Munich 2025

Voting

: eight minus four?
(Example: nine)

The Note You're Voting On

Anonymous
7 years ago
This function will attempt to parse relative URLs but relaying on it can produce unexpected behavior that can cause some hard to track bugs. (The following results are obtained from PHP 5.5.19)

Attempting to parse a url like this
http://example.com/entities/GOA:98/?search=8989157d1f22
Correctly produces
<?php
array (
'scheme' => 'http',
'host' => 'example.com',
'path' => '/entities/GOA:98/',
'query' => 'search=8989157d1f22',
);
?>

However, Attempting to parse the relative URL
entities/GOA:98/?search=8989157d1f22
<?php
array (
'host' => 'entities',
'port' => 98,
'path' => '/GOA:98/',
'query' => 'search=8989157d1f22',
)
?>
If I change :98 to :A98 parse_url parses the URL correctly as
<?php
array (
'path' => 'entities/GOA:A98/',
'query' => 'search=8989157d1f22',
)
?>
Bottom line, Avoid using parse_url for relative urls unless you have tested the expected input and you know parse_url will handle them well.

https://forums.hawacastle.com/

<< Back to user notes page

To Top