通过curl设置cookie
我试图在 PHP 中通过 cURL 设置 cookie,但失败了。我的 php 代码看起来像这样,
$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;
?>
文件 test_cookies.html 包含检查 cookie 的 javascript,如果发现它会显示带有附加用户内容的内容。 但是当我使用上面的脚本时,它显示页面 test_cookies.html 的内容,但不显示其他用户内容,这意味着它没有设置 cookie。
我尝试编写另一个像这样的脚本
<?php
header("Set-Cookie:user=1");
header("Location:test_cookies.html");
?>
,它可以工作并设置 cookie 并显示其他用户内容。 我还尝试使用
curl_setopt($ch,CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt($ch,CURLOPT_COOKIEJAR,"cookie.txt");
此方法将 cookie 信息写入文件,但在获取页面时不读取它。 有人可以帮忙吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于 javascript 在浏览器中进行检查,因此您应该在将输出发送到浏览器之前设置 cookie。所以你需要结合两个脚本:
说明:
请注意,我们在这里查看两次传输:
这是一种特殊情况,您使用curl从
localhost
获取内容,但在现实生活中,您会使用curl从第三个主机获取内容。如果您根据请求中是否发送 cookie 收到不同的内容,那么您应该使用curl 设置 cookie。在大多数情况下,您可以将内容发送到浏览器,无需进行额外的调整。但在这里,您通过检查浏览器中的 cookie 来做出决定,因此您不需要第一个 cookie 设置,而您确实需要第二个。
Since javascript conducts the check in the browser, you should set the cookie before sending the output to the browser. So you need to combine both scripts:
Explanation:
Please note that we are looking at two transfers here:
This is a special case where you are using curl to get the content from
localhost
, but in real-life uses you'd use curl to get content from a 3rd host.If you receive different content based on whether a cookie is sent in the request or not, then you should set the cookie with curl. In most cases you can then send the content with no additional tweaking to the browser. But here, you make the decision with checking for a cookie in the browser, so you don't need the first cookie setting, and you do need the second one.
JavaScript 无法通过curl 获取页面来工作。
JavaScript will not work by getting page from curl.