在mysql中存储卷曲会话cookie

时间:2012-03-17 19:02:47

标签: php mysql cookies curl

我想知道是否可以在mysql中存储curl的会话cookie。

curl_setopt($ch, CURLOPT_COOKIEJAR, get_the_cookie());
curl_setopt($ch, CURLOPT_COOKIEFILE, get_the_cookie());

get_the_cookie()返回一个unique-to-user.txt文件路径, 但我希望能够将这个cookie存储在mysql而不是文件系统中,如果可能的话。

1 个答案:

答案 0 :(得分:4)

cURL不允许您直接执行此操作,但您可以通过在请求期间创建临时文件并根据需要手动将其内容从/向数据库传输来伪造它。

例如:

$cookiejar = // get cookies from database
$cookiejarfile = tempnam(sys_get_temp_dir());
$cookiefile = tempnam(sys_get_temp_dir());
file_put_contents($cookiejarfile, $cookiejar);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejarfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);

$newcookies = file_get_contents($cookiefile);

// and now save cookies to database and clean up temp files