Sending Cookies Back via CURL in PHP
Cookies saved in a file can be automatically
sent back on the next request.
This is done using two options:
the CURLOPT_COOKIEJAR option
commands to accept and save cookies
to a file, and the CURLOPT_COOKIEFILE option
commands to send the saved
cookies to the server.
Let's set these options:
<?php
$cookieFilePath = $_SERVER['DOCUMENT_ROOT'] . '/cookie.txt';
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFilePath);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFilePath);
?>
Make two requests to the following page:
<?php
if (!empty($_COOKIE)) {
echo date('H:i:s', $_COOKIE['time']);
} else {
setcookie('time', time(), time() + 3600, '/');
echo 'cookie saved';
}
?>
Make sure that on the first request the cookie is saved to the file, and on the second - it is sent back.