Adding cookie header causes request to hang
P粉897881626
P粉897881626 2023-09-15 21:20:14
0
1
605

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粉897881626
P粉897881626

reply all(1)
P粉041856955

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!