skip to Main Content

The following script (stripped down for clarity and simplicity) has run fine for years, but since upgrading from php 5 to 8, the cookie file created is no longer deleted by the unlink command, despite it reporting success, leading to a build-up of these cookie files.

If I take out the CURLOPT_COOKIEJAR line, then the file is deleted just fine.

<?php
    $my_cookie=tempnam("./","XX");
    
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $my_cookie);
    curl_close($ch);    
    
    if (unlink($my_cookie)) {    
        echo "success - $my_cookie unlinked<BR>";
    } else {
        echo "fail";    
    }
?>

Any ideas?

2

Answers


  1. Try using an absolute path: curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath($my_cookie) );

    Login or Signup to reply.
  2. curl_close($ch); does nothing in PHP >= 8.0.0, so the cookie file doesn’t actually exist (yet) when you’re trying to delete it.

    Replace curl_close($ch); with unset($ch);. This will write the cookie file.

    See documentation of curl_close:

    Note:
    This function has no effect. Prior to PHP 8.0.0, this function was used to close the resource.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search