Reputation: 523
I am tring to read a cookie value which I got after login by sending a POST request.
Then I want to sent that cookie value with another POST request using Curl to another action. But after sending this when I am trying to see all posted header it does display that I have send any cookie value. This value is not available to my posted URL so not able to access the information due to authentication. Please tell me where I have done something wrong:
$URL1 = "http://www.getinf.com/iconf/user?action=buGroup";
$postfields1 = "device=mapp&type=ajax&name1=ra&cc1=91&min1=90name2=imm&cc2=91&min2=97";
// sends a post request
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL,$URL1);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_COOKIE,'JSESSIONID=199FFF6355DEA87F3D72E692E7514AD2');
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch1, CURLOPT_HEADER,true);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $postfields1);
$result = curl_exec($ch1);
print_r(headers_list());// displays all post request data
$ret = ReturnVal($result);
print_r(get_headers($URL1, 1)); //
curl_close ($ch1);
So what is wrong in this code that is preventing JSESSIONID value accessible as a cookie value?
Upvotes: 2
Views: 2340
Reputation: 337
CURLOPT_COOKIESESSION is used to start a new Cookie session and ignore all cookies stored.
From PHP.net: Use CURLOPT_COOKIESESSION = TRUE to mark this as a new cookie "session". It will force libcurl to ignore all cookies it is about to load that are "session cookies" from the previous session. By default, libcurl always stores and loads all cookies, independent if they are session cookies or not. Session cookies are cookies without expiry date and they are meant to be alive and existing for this "session" only.
use: curl_setopt($ch, CURLOPT_COOKIESESSION, false);
Upvotes: 0
Reputation: 56948
Check the comments (or search "cookie") on this page in the php docs:
Whats not mentioned in the documentation is that you have to set CURLOPT_COOKIEJAR to a file for the CURL handle to actually use cookies, if it is not set then cookies will not be parsed.
Upvotes: 1