Cannot send 'PHPSESSID' via file_get_contents && stream_context_create

953 views Asked by At

I am trying send a http request but I've noticed that when I am trying to send PHPSESSID as COOKIE I get the warning:

file_get_contents('http://mysite.localhost'): failed to open stream: HTTP request failed

Code:

$options = array(
    'http' => array(
        'header'  => 
            "Content-type: application/x-www-form-urlencoded\r\n".
            "Cookie: {$this->COOKIE}\r\n",
                'method'  => $this->method,
                'content' => $this->POST,
            ),
        );
$c = file_get_contents($this->uri, false, stream_context_create($options));
if(!$c)
    throw new \Exception("Unable to fetch the request...");
echo $c;

EDITED
var_dump ouput of :

var_dump($this->COOKIE, $this->method, $this->POST);

IS

string(53) "HI=COOKIE; L=S; PHPSESSID=o1eqt811isceh4f0nhfnnlnm70"
string(4) "POST"
string(6) "POST=VALUE"

What am I doing wrong?

2

There are 2 answers

0
billoo On BEST ANSWER

you must close the current session first, just before calling the function "file_get_contents" call this function "session_write_close".

do not forget to reopen session_start after calling "file_get_contents" if you want to use sessions after that calling.

so the code will be like this:

$options = array(
    'http' => array(
        'header'  => 
            "Content-type: application/x-www-form-urlencoded\r\n".
            "Cookie: {$this->COOKIE}\r\n",
                'method'  => $this->method,
                'content' => $this->POST,
            ),
        );
session_write_close();
$c = file_get_contents($this->uri, false, stream_context_create($options));
1
Baraa Al-Tabbaa On

be sure that allow-url-fopen is "on" inside your php.ini configuration