Set cookie through curl

I am trying to set a cookie via cURL in PHP, but it fails. my php code is as follows

$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 test_cookies.html file contains javascript that checks for the presence of a cookie and if it detects that it is sending content with additional user content. but when I use the above script, it displays the contents of the test_cookies.html page, but not with additional user content, which means that it does not set the cookie.

I tried to write another script like this

<?php
header("Set-Cookie:user=1");
header("Location:test_cookies.html");
?>

it works and sets a cookie and displays additional user content. I also tried using

curl_setopt($ch,CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt($ch,CURLOPT_COOKIEJAR,"cookie.txt");

this writes the cookie information to the file but does not read it when the page is retrieved. can anyone help?

+2
2

javascript , cookie . :

$ch=curl_init();
$url="http://localhost/javascript%20cookies/test_cookies.html";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
header("Set-Cookie:user=1");
echo $contents;

:
, :

  • curl,
  • .

, curl localhost, curl .

, cookie , cookie curl. . , cookie , cookie, .

+2

JavaScript , .

+1

Source: https://habr.com/ru/post/1724954/


All Articles