html页面无法通过libcurl获取cookie

发布于 2024-11-07 05:27:42 字数 1856 浏览 0 评论 0原文

我修改了之前的代码。如果您有兴趣通过curl设置cookie,您可以查看我之前的帖子 但这是一个新的开始,我的新代码看起来与此相关 我有一个像这样使用curl的php文件

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

,文件test_cookies.php看起来像这样,

<?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,就会显示相应的文本。 带有curl代码的php文件正在发送cookie,test_cookies.php正在设置cookie并重定向到页面test_cookies.html > 但此页面未接收 cookie,因此不会相应地显示内容。 有人可以告诉我现在有什么问题吗?

这是我在将 CURLOPT_HEADER 设置为 true 时在 Firefox 中显示的标头,

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 次调用吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满地尘埃落定 2024-11-14 05:27:42

目前尚不清楚您想通过代码实现什么目的。如果你解释一下为什么要这样做,你会得到更好的帮助。您的方法很可能是错误的,您可以使用不同/更简单的方法获得最终结果。

现在回答你的问题,发生的情况是这样的:

  1. 从你的浏览器,你发送一个 GET 请求到 curl.php (我为你的第一个文件使用的名称),
  2. curl 在该文件中检查 cookie.txt 并找到 user=1,因此它向 test_cookies.php 发送 GET 请求,并通过请求
  3. test_cookies 发送 cookie。 php 看到 user=1 为 true,因此它向 curl.php 发送一个标头,并要求它设置两个 cookie:user=1color=blue (对于第一个 cookie,这是毫无意义的,user=1 已经设置,但我们忽略它,因为没有造成任何损害)
  4. 您尚未设置 CURLOPT_COOKIEJAR< /code> 选项,因此当 curl.php 接收到设置的 cookie 标头时,它不会给出该死的(什么也没有发生)
  5. 接下来,test_cookies.php 将重定向标头发送到 < code>curl.php,由于您设置了CURLOPT_FOLLOWLOCATION,1curl.php发送另一个GET请求,这次获取test_cookies.html
  6. test_cookies.html的内容返回到curl.php
  7. curl_exec($ch);导致返回的内容(来源test_cookies.html)被回显到您的浏览器,
  8. 您的浏览器解析它收到的内容并执行 JavaScript。它检查名为 user 的 cookie,但没有找到,因此它会在不存在 cookie 时显示内容(因为不存在)。

现在,您可能想知道如果添加会发生什么:

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

您的 cookie.txt 将在第 4 步中更新,并且将有两个 cookie; user=1color=blue。但这并没有给你你期望的结果。因为header("Set-Cookie:user=1; color=blue");指示curl设置cookie,而curl通过将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 中。

It is not clear what you are trying to achieve with the code. You'd get better help if you explain why you are doing this. It is probable that your approach is wrong and you could get the ultimate result with a different/simpler approach.

And now to answer your question, this is what happens:

  1. From your browser, you send a GET request to curl.php (the name I use for your first file),
  2. curl in that file checks cookie.txt and finds user=1, so it sends a GET request to test_cookies.php and sends the cookie with the request
  3. test_cookies.php sees that user=1 is true, so it sends a header to curl.php and asks it to set two cookies: user=1 and color=blue (for the first cookie this is pointless, user=1 is already set, but let's ignore this as no harm is done)
  4. You have not set CURLOPT_COOKIEJAR option, so when curl.php receives the set cookie header it does not give a damn (nothing happens)
  5. Next, test_cookies.php sends a redirect header to curl.php, since you have set CURLOPT_FOLLOWLOCATION,1, curl.php sends another GET request, this time to get test_cookies.html
  6. Content of test_cookies.html is returned to curl.php,
  7. curl_exec($ch); causes the returned content (source of test_cookies.html) be echoed back to your browser,
  8. Your browser parses what it received and the javascript is executed. It checks for a cookie named user and does not find one, so it displays content for when there is no cookie (because there isn't).

Now, you may wonder what happens if you add:

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

What happens is that your cookie.txt will be updated in step 4 and will have two cookies; user=1 and color=blue. But this does NOT give you the result you expect. Because header("Set-Cookie:user=1; color=blue"); is instructing curl to set the cookie, and curl does this by storing the cookies in the file you specified in CURLOPT_COOKIEJAR option. So, even though you added that option, when you reach step 8 javascript will not find that cookie, because the browser you use does not know or care about your cookie.txt, it looks somewhere else to check if a cookie exists. In Google Chrome for instance the format is SQLite and for XP the cookies are stored in %USERPROFILE%\Local Settings\Application Data\Google\Chrome\User Data\Default\Cookies.

秋凉 2024-11-14 05:27:42

我现在将解释我正在尝试做什么。
我有一个 html 页面,要求一些验证码,然后将该验证码发送到一个 php 脚本,该脚本在验证代码时设置一个 cookie 并重定向到同一个 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");

和现在带有curl代码的php文件

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的知识。
感谢所有在我黑暗的时候帮助过我的人。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文