如何阻止 php curl 重写 cookie
How to stop php curl from rewriting cookies
我的代码:
<?php
function access_page($url,$data){
$fp = fopen("cookie.txt", "w");
fclose($fp);
$login = curl_init();
curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($login, CURLOPT_TIMEOUT, 40000);
curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($login, CURLOPT_URL, $url);
curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($login, CURLOPT_POST, TRUE);
curl_setopt($login, CURLOPT_POSTFIELDS, $data);
curl_setopt($login, CURLOPT_ENCODING ,"");
ob_start();
return curl_exec ($login);
ob_end_clean();
curl_close ($login);
unset($login);
}
?>
每次我 运行 使用此功能时,我的 cookie.txt 文件都会被删除并用新的 cookie 重写,即使 cookie 不冲突也是如此。
例如:
access_page("http://examplepage.com/session","");
access_page("http://examplepage.com/login","username=123&password=123");
我希望它在我的 cookie.txt 文件中同时写入会话和登录 cookie,但是即使 cookie 不同,第一个请求也会被第二个请求重写。
如何将这两种饼干都保存在饼干罐中?
来自 fopen 手册页
'w' Open for writing only; place the file pointer at the beginning of
the file and truncate the file to zero length. If the file does not
exist, attempt to create it.
(大胆强调我的)。
所以这些行实际上删除了所有内容,所以删除这些行...
$fp = fopen("cookie.txt", "w");
fclose($fp);
我的代码:
<?php
function access_page($url,$data){
$fp = fopen("cookie.txt", "w");
fclose($fp);
$login = curl_init();
curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($login, CURLOPT_TIMEOUT, 40000);
curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($login, CURLOPT_URL, $url);
curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($login, CURLOPT_POST, TRUE);
curl_setopt($login, CURLOPT_POSTFIELDS, $data);
curl_setopt($login, CURLOPT_ENCODING ,"");
ob_start();
return curl_exec ($login);
ob_end_clean();
curl_close ($login);
unset($login);
}
?>
每次我 运行 使用此功能时,我的 cookie.txt 文件都会被删除并用新的 cookie 重写,即使 cookie 不冲突也是如此。
例如:
access_page("http://examplepage.com/session","");
access_page("http://examplepage.com/login","username=123&password=123");
我希望它在我的 cookie.txt 文件中同时写入会话和登录 cookie,但是即使 cookie 不同,第一个请求也会被第二个请求重写。 如何将这两种饼干都保存在饼干罐中?
来自 fopen 手册页
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
(大胆强调我的)。
所以这些行实际上删除了所有内容,所以删除这些行...
$fp = fopen("cookie.txt", "w");
fclose($fp);