Search code examples
phphttpcurlrequest-headers

How to read the php request header values?


so I have to read the curl header request value in PHP, basically, I have to take the CRSF token and cookie value from the request header and then post those values in the post header to bypass the login authentication.

I tried header out and header true in cURL options but it only retrieve the response header value.

curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLINFO_HEADER_OUT, true); 

Values I want to read from request header:

enter image description here


Solution

  • From your screenshot (which appears to be from a browser's Network tool) it looks like you are talking about reading the header values which were received by the PHP script from the request your browser sent to PHP. That's nothing to do with cURL - cURL is for sending HTTP requests from your PHP script to another URL...it's unrelated to the interaction between the client-side and PHP via your webserver.

    To read the headers which are incoming into your PHP script from the browser (or other client) making the request, you can use getallheaders() which returns an associative array of all the received headers.

    e.g. to simply list them all:

    foreach (getallheaders() as $name => $value) {
        echo "$name: $value\n";
    }
    

    Documentation: https://www.php.net/manual/en/function.getallheaders.php