简体   繁体   中英

Permanently save cURL cookie

Is it possible to permanently store a cookie so I can reuse it? For now I have to relogin everytime I run a script to access several pages which requires the user to be logged in, and I was wondering if it's possible to store the cookie permanently so I don't have to relogin everytime.

$post = $metadata;
$ch = curl_init('login.php');
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $post,    
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3',
    CURLOPT_COOKIEFILE => 'cookie.txt',
    // CURLOPT_COOKIE => '',
    CURLOPT_COOKIEJAR => 'cookie.txt'
));
$result = curl_exec($ch);
curl_close($ch);

I specified a cookie.txt file but I don't actually see it.

Create the file cookie.txt before setting in it CURLOPT_COOKIEFILE.

CURLOPT_COOKIEJAR will create the file for you, but CURLOPT_COOKIEFILE requires an already created file for sending it as a request.

Also be sure, that you have write and read permissions on that file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM