Tilbagesendelse af cookies via CURL i PHP

Gemte cookies i en fil kan automatisk sendes tilbage ved næste anmodning. Dette gøres ved hjælp af to indstillinger: indstillingen CURLOPT_COOKIEJAR befaler at modtage og gemme cookies i en fil, og indstillingen CURLOPT_COOKIEFILE befaler at sende de gemte cookies til serveren.

Lad os sætte disse indstillinger:

curl.php<?php
  $cookieFilePath = $_SERVER['DOCUMENT_ROOT'] . '/cookie.txt';
  
  curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFilePath);
  curl_setopt($curl, CURLOPT_COOKIEJAR,  $cookieFilePath);
?>
Задание 1

Foretag to anmodninger til følgende side:

file.php<?php
  if (!empty($_COOKIE)) {
    echo date('H:i:s', $_COOKIE['time']);
  } else {
    setcookie('time', time(), time() + 3600, '/');
    echo 'cookie gemt';
  }
?>

Sikr dig, at cookie'en ved den første anmodning gemmes i filen, og ved den anden - sendes tilbage.