search

Home  >  Q&A  >  body text

Adding cookie header causes request to hang

I am working on a class project where we need to write a website using php. We were told that we should write separate pages to query information and a php session for saving variables. I have successfully started a session, but when I try to make a request and add headers for the session, the request stalls and fails to complete. Below is the code I'm using.

function GetDataV2(string $URL, string $method, array $postPayload)
{
    $sessID = $_COOKIE['PHPSESSID'];
    $cookieString = "Cookie: PHPSESSID=$sessID";

    $options = array(
        'http' => array(
            'header' => array('Content-Type: application/x-www-form-urlencoded', 'Accept: application/json', $cookieString),
            'method' => 'POST',       
            'content' => http_build_query($postPayload)
        )
    );

    $context = stream_context_create($options);

    return file_get_contents($URL, false, $context);
}

Any help would be appreciated as I can't seem to figure out what is causing the request to hang. Unfortunately, this project requires php, otherwise I wouldn't use it at all.

P粉897881626P粉897881626480 days ago652

reply all(1)I'll reply

  • P粉041856955

    P粉0418569552023-09-16 19:31:05

    After searching more extensively, I need to add session_write_close() before executing the call to release the session file before other pages can modify it.

    reply
    0
  • Cancelreply