HTML page does not receive cookies via libcurl

i changed my previous code. you can see my previous post if your intersted set a cookie through curl But here is a new beginning my new code looks like this I have a php file using curl like this

<?php
$ch=curl_init();
$url="http://localhost/javascript%20cookies/test_cookies.php";
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_COOKIEFILE,dirname(__FILE__) . "/cookie.txt");
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_exec($ch);
curl_close($ch);
?>

the file test_cookies.php is as follows

<?php
if($_COOKIE['user']==1)
{
header("Set-Cookie:user=1; color=blue");
header("Location:http://localhost/javascript%20cookies/test_cookies.html");
}
?>

test_cookies.html javascript, cookie, cookie, . php curl- cookie, test_cookies.php cookie test_cookies.html, cookie , . - , ?

, firefox CURLOPT_HEADER true

HTTP/1.1 302 Found Date: Mon, 16 May 2011 15:03:59 GMT Server: Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1 X-Powered-By: PHP/5.3.1 Set-Cookie: user=1; color=blue Location: http://localhost/javascript%20cookies/test_cookies.html Content-Length: 0 Content-Type: text/html HTTP/1.1 200 OK Date: Mon, 16 May 2011 15:03:59 GMT Server: Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1 Last-Modified: Mon, 16 May 2011 12:13:24 GMT ETag: "11000000013d0c-493-4a363950a70f3" Accept-Ranges: bytes Content-Length: 1171 Content-Type: text/html

, . , 2 ?

0
2

, . html-, , PHP , html. html javascript, cookie .

html

test_cookies.html

    //some css,javascript and html and then a form
<form method="post" action="http://localhost/javascript%20cookies/test_cookies.php">

php,

test_cookies.php

if($_POST['value']=="code")
setcookie("user",1);
if($_POST['value']!="code")
setcookie("user",1,time()-1);
header("Location:http://localhost/javascript%20cookies/test_cookies.html");

php curl-

curl_cookies.php

<?php
$ch=curl_init();
$url="http://localhost/javascript%20cookies/test_cookies.php";
$post="value=code";     //here i have hard-coded the pst value for a demo but i could have got this from the user
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$res=curl_exec($ch);
curl_close($ch);
preg_match('/Location: (.*)\s/',$res,$location);
preg_match('/Set-Cookie: (.*)\s/',$res,$cookie);
$cookie=rtrim($cookie[0])."; path=/ ";         //path needed to be changed because curl_cookies.php and test_cookies.html are in different directories. 
header($cookie);
header($location[0]);
?>

- , . http. , , .

+1

, . , , . , , / .

, , :

  • GET curl.php (, ),
  • curl cookie.txt user=1, GET test_cookies.php cookie
  • test_cookies.php , user=1 , curl.php cookie: user=1 color=blue ( cookie , user = 1 , , )
  • CURLOPT_COOKIEJAR, , curl.php cookie, ( )
  • test_cookies.php curl.php, CURLOPT_FOLLOWLOCATION,1, curl.php GET, , test_cookies.html
  • test_cookies.html curl.php,
  • curl_exec($ch); ( test_cookies.html) ,
  • , , javascript. cookie user , , cookie ( ).

, , :

curl_setopt($ch,CURLOPT_COOKIEJAR,dirname(__FILE__) . "/cookie.txt");

, cookie.txt 4 ; user=1 color=blue. , . header("Set-Cookie:user=1; color=blue"); curl cookie, , cookie , CURLOPT_COOKIEJAR. , , 8, javascript cookie, cookie.txt, , , cookie. Google Chrome, , SQLite XP cookie %USERPROFILE%\Local Settings\Application Data\Google\Chrome\User Data\Default\Cookies.

+1

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


All Articles