通过curl设置cookie

发布于 2024-11-07 15:26:48 字数 849 浏览 2 评论 0原文

我试图在 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 技术交流群。

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

发布评论

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

评论(2

北方的巷 2024-11-14 15:26:48

由于 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;

说明:
请注意,我们在这里查看两次传输:

  1. 通过curl 获取数据,然后
  2. 将其发送到浏览器。

这是一种特殊情况,您使用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:

$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;

Explanation:
Please note that we are looking at two transfers here:

  1. data is fetched by curl, and then
  2. it is sent to the browser.

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.

两个我 2024-11-14 15:26:48

JavaScript 无法通过curl 获取页面来工作。

JavaScript will not work by getting page from curl.

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