I'm developing a login with curl and I'm making two requests, the first one makes the post to the action page of the form and generates a txt file with the cookie, soon after I make a request using that file to access the protected page. but I do not have access to the page, the system redirects to the error page.
$tmpfname = dirname(__FILE__).'/cookies.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname );
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname );
$redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_setopt($ch, CURLOPT_ENCODING, "");
$err = curl_error($ch);
$ret = curl_exec($ch);
curl_close ($ch);
$chl = curl_init();
curl_setopt($chl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($chl, CURLOPT_COOKIEFILE, $tmpfname );
curl_setopt($chl, CURLOPT_URL, $urlll);
curl_setopt($chl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($chl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($chl, CURLOPT_HEADER, 1);
curl_setopt($chl, CURLOPT_VERBOSE, 1);
curl_setopt($chl, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($chl, CURLOPT_TIMEOUT, 120);
curl_setopt($chl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($chl, CURLOPT_ENCODING, "");
$er = curl_error($chl);
$hy = curl_exec($chl);
curl_close ($chl);
var_dump($hy);
echo $hy;