douli2063 2011-05-16 15:39
浏览 87
已采纳

html页面没有通过libcurl获取cookie

i modified my previous code. you can see my previous post if your intersted setting cookie through curl But here is a fresh beginning my new code looks linke 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 looks like this

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

the file test_cookies.html has some javascript that checks for cookies and if it finds those cookies then it displays the text accordingly. the php file with curl code is sending the cookies and the test_cookies.php is setting the cookie and redirecting to the page test_cookies.html but this page is not receiving the cookies and thus it is not not showing the content accordingly. can somebody tell me whats the problem now?

here are the headers i get displayed in firefox on setting CURLOPT_HEADER to 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

you can see that there are two set of headers displayed.is this because i am making 2 calls to header?

  • 写回答

2条回答 默认 最新

  • dql1978dql1978 2011-05-18 11:11
    关注

    i will now explain what i was trying to do. i had an html page that asked for some verification code and then it sent that verification code to a php script which on verifying the code set a cookie and redirected to the same html page. the html page had some externally linked javascript which on checking the cookie value displayed the content of the page with some tweaking. i am now writing the code for this

    the html file with content and form

    test_cookies.html

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

    the php file which verifies the code

    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");
    

    and now the php file with curl code

    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]);
    ?>
    

    this finally worked and the browser is displaying the tweaked content. this thing taught me a lot about http. Thanks to all who helped me when i was in dark.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名