php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71245 file_get_contents() ignores "header" context option if it's a reference
Submitted: 2015-12-30 11:09 UTC Modified: -
From: php at maisqi dot com Assigned:
Status: Closed Package: Streams related
PHP Version: 7.0.1 OS: Windows 64 bits
Private report: No CVE-ID:
 [2015-12-30 11:09 UTC] php at maisqi dot com
Description:
------------
file_get_contents() does not pass the headers in the stream context if it's given as a reference to an array, as exemplified below:

  $headers = ['Host: maisqi.com'];
  $httpContext = [
     'http' => [
        'header' => &$headers,
     ],
  ];
  $uri = 'http://77.91.206.217/outros/bugs/php/_print_host_headers.php';
  $content = file_get_contents($uri, false, stream_context_create($httpContext));

In PHP 5.6, this works flawlessly; on PHP7, the request is issued with no headers. If no reference is given ('header' => $headers), this works on both versions.


Test script:
---------------
$headers = ['Host: maisqi.com'];
$httpContext = [
	'http' => [
		'protocol_version'	=> '1.1',
		'method'			=> 'GET',
		'header'			=> &$headers,
		'follow_location'	=> 0,
		'max_redirects'		=> 0,
		'ignore_errors'		=> true,
		'timeout'			=> 60,
	],
];

$uri = 'http://77.91.206.217/outros/bugs/php/_print_host_headers.php';
$content = file_get_contents($uri, false, stream_context_create($httpContext));

echo "Content:\n";
if ($content === false)
	echo '-';
else if ($content === '')
	echo '(empty)';
else
	echo htmlspecialchars($content);

echo "\n\n\$http_response_header:\n";
if (empty($http_response_header))
	echo '-';
else
	print_r($http_response_header);


Expected result:
----------------
It should print something like tbis:
  Content:
  HTTP_HOST: maisqi.com
  SERVER_NAME: maisqi.com


  $http_response_header:
  Array
  (
      [0] => HTTP/1.1 200 OK
      [1] => Date: Wed, 30 Dec 2015 11:04:09 GMT
      [2] => Server: Apache
      [3] => Connection: close
      [4] => Content-Type: text/html; charset=utf-8
  )


Actual result:
--------------
It prints the HTML of a "Page Not Found" page, because no specific Host header is requested.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-30 13:04 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=90d2f509e2164b60fc7bc8407e63504c910cce11
Log: Fixed bug #71245 (file_get_contents() ignores "header" context option if it's a reference)
 [2015-12-30 13:04 UTC] laruence@php.net
-Status: Open +Status: Closed
 [2016-07-20 11:34 UTC] davey@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=90d2f509e2164b60fc7bc8407e63504c910cce11
Log: Fixed bug #71245 (file_get_contents() ignores "header" context option if it's a reference)
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Feb 21 19:01:38 2017 UTC