phpcookieshttp-headersfsockopen

How to send cookie alongside http request from cookie.txt using fsockopen?


I was searching on how to use curl (send http request) without waiting for the response to complete, because i just want to trigger many action and dont expect the results. After time of searching, I found a solution Here, but i have a problem.
The problem is i don't know how to pass cookie that I have saved in .txt and i want to send it using this request.

function curl_post_async($url, $params)
{
    foreach ($params as $key => &$val) {
        if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }

    $post_string = implode('&', $post_params);

    $parts=parse_url($url);

    $fp = fsockopen($parts['host'],
    isset($parts['port'])?$parts['port']:80,
    $errno, $errstr, 30);

    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}

How to send cookie from .txt files using above's code?


Solution

  • After research I solved how to send cookies from cookies.txt
    When I search I found 2 formats of cookies
    1. Netscape

    # Netscape HTTP Cookie File
    # https://curl.haxx.se/docs/http-cookies.html
    # This file was generated by libcurl! Edit at your own risk.
    
    #HttpOnly_.site.com TRUE    /   FALSE   1517538526  __cfduid    dc0d1a86523f4fa68c7c9e19b084aa1651486002526
    #HttpOnly_www.site.com  FALSE   /   FALSE   1486006126  cisession   a914d92115f6a6d73124ac5de4c49e070b872b98
    

    2. Cookie default format (reference: Here)

    csrf_cookie_name=cac551cc13952929efd2507797fff687; ci_session=umt92quvucfu333p1co19b83i3jaglm9;
    

    I tried to send the cookie format no.1 using this line code

    $out.= "Cookie: ".file_get_contents(getcwd().$cookie); //where getcwd().$cookie is location of the cookie.txt with format no.1
    


    I tried that but it failed. But if the cookie.txt's cookie value is format no.2 I sent that cookie and it successfully sended and can be read.
    Conclusion to send cookie:

    $out.= "Cookie: csrf_cookie_name=cac551cc13952929efd2507797fff687; ci_session=umt92quvucfu333p1co19b83i3jaglm9;";