I am trying to set cookie through cURL in PHP but it fails. my php code looks like this
$ch=curl_init();
$url="http://localhost/javascript%20cookies/test_cookies.html";
curl_setopt($ch, CURLOPT_COOKIE, 'user=1');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
?>
the file test_cookies.html contains javascript that checks for a cookie and if it finds it dispalys the content with additional user content. but when i use the above script it displays the contents of the page test_cookies.html but not with the additional user content which means that it is not setting the cookie.
i tried writing another script like this
<?php
header("Set-Cookie:user=1");
header("Location:test_cookies.html");
?>
this works and sets the cookie and shows the additional user content too. I also tried using
curl_setopt($ch,CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt($ch,CURLOPT_COOKIEJAR,"cookie.txt");
this is writing the cookie information to the file but not reading it when fetching the page. can somebody help?