使用`curl_setopt`等在另一个文件中设置cookie


set cookie in onether file with `curl_setopt` etc

我正在谷歌上搜索,试图找到问题的解决方案。

我需要在不同的文件中设置一个cookie,我正在尝试使用curl_setopt,但它不起作用。

无论如何,这个想法都是用PHP在另一个页面中发送设置cookie的值,类似于这样:

    file1.php
    <? //start php
    //at the begining of the file i have
session_start();
header('Content-Type: text/html; charset=utf-8');
//if i set a cookie now it give me an error cause i can not change the header
//but because i need to set a cookie now without leaving this file
//i tryed to set it in file2.php this way:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $SITE_HOME_DIR ."login.php");
// Do a POST
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email=pok@pok.com'); 
curl_close($ch);
    ///end php
    ?>
    file2.php
    <?
//just set cookie
setcookie("TestCookie", $_POST['email'], time()+3600)
?>

但这不起作用。。。。

有什么想法吗?非常感谢。

您只能使用setcookie()为下一个脚本设置cookie。别无选择。——cURL向其他页面发送不同的请求,但不可能在客户端(浏览器)设置任何cookie。

您的错误在已发送的邮件头(参考答案)中有解释。如果你不能/不会重写你的页面,你可能会也可能不会幸运地使用上面提到的ob_start()解决方法。