Skip to content

file_get_contents should probably clear the global state of last headers #19727

@Seldaek

Description

@Seldaek

Description

The following code:

<?php

file_get_contents(__FILE__);
var_dump(http_get_last_response_headers());

file_get_contents('https://php.net/');
var_dump(http_get_last_response_headers());

file_get_contents(__FILE__);
var_dump(http_get_last_response_headers());

Resulted in this output:

NULL
array( headers from php.net )
array( headers from php.net )

But I expected this output instead:

NULL
array( headers from php.net )
NULL // as reading a file clearly outputs no headers

IMO this is very surprising, and can lead to bugs (see jsonrainbow/json-schema#843 for example) as people are now nudged to switch to http_get_last_response_headers() by the deprecations.

The problem is that $http_response_headers, while a very ugly API, was a local variable.. and http_get_last_response_headers() while appearing cleaner stores global state and can thus leak data across boundaries if you do not call http_clear_last_response_headers() after fetching the headers.

Thus my suggestion would be that file_get_contents first clears the headers and then populates them if the stream wrapper in use has anything to populate.

/cc @Girgias as I know you were involved in the deprecation. And apologies if this was already discussed somewhere I didn't find.

PHP Version

All versions where `http_get_last_response_headers()` is available

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions