Last Updated: February 25, 2016
·
391
· yaodong

PHP stream_context_get_default() overrides default context

PHP has a function named stream_context_set_default(), however, another function stream_context_get_default() overrides default contexts too.

$default_opts = [
  'http' => [
    'method' => "GET",
    'header' => "Accept-language: en\r\nCookie: foo=bar"
  ]
];

$default = stream_context_get_default($default_opts);

readfile('http://localhost');

The server will get an request includes:

[
  "Accept-language" => "en",
  "Cookie" => "foo=bar"
]