cURL 选项 - 会话和 cookie

cURL Option - session and cookie

在一个脚本中,我使用 cURL 进行登录或 post 一些信息到另一个站点。

当一个用户加载这个脚本时,我们没有任何问题,但是当两个用户同时加载脚本时,另一个站点加载第一个页面,因为我们主机中的 cookie 为一个用户设置,并且该站点从一个用户获得两个不同的命令用户。

当为任何用户加载脚本时网站为每个用户获得不同的 SESSION 和 COOKIE,我该如何做到这一点?

$ch = curl_init();
$agent = $_SERVER["HTTP_USER_AGENT"];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
$result = curl_exec($ch);
return $result;  

如果你有每个用户的用户 ID,你可以做类似的事情

用用户 table 的 ID 填写 $user_id 然后

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie'.$user_id.'.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie'.$user_id.'.txt');