# PHP package for managing HTTP cookies

The HttpSoft\Cookie package provides convenient cookie management in accordance with the RFC 6265 specification.

This package supports PSR-7 and PSR-15 interfaces.

This package requires PHP version 7.4 or later.

Package installation:

composer require httpsoft/http-cookie

Source code on GitHub.



# Usage

use HttpSoft\Cookie\Cookie;
use HttpSoft\Cookie\CookieCreator;
use HttpSoft\Cookie\CookieManager;
use HttpSoft\Cookie\CookieSendMiddleware;

 * @var HttpSoft\Emitter\EmitterInterface $emitter
 * @var Psr\Http\Message\ResponseInterface $response
 * @var Psr\Http\Message\ServerRequestInterface $request
 * @var Psr\Http\Server\RequestHandlerInterface $handler

$manager = new CookieManager();

// Create cookie
$cookie1 = new Cookie('test', 'value', '+1 hour');
// or
$cookie2 = CookieCreator::create('test2', 'value', time() + 3600, '.example.com', '/path');
// or from raw `Set-Cookie` header
$cookie3 = CookieCreator::createFromString('name=value; Path=/; Secure; HttpOnly; SameSite=Lax; ...');

// Set cookies to the manager

// Set all cookie to the response for sending
$response = $manager->send($response);
// or use `CookieSendMiddleware` middleware
$middleware = new CookieSendMiddleware($manager);
$response = $middleware->process($request, $handler);

// Emit a response to the client

// Alternative if not using PSR-7
// Set all cookie for sending using `setcookie()` function
foreach ($manager->getAll() as $cookie) {
    setcookie($cookie->getName(), $cookie->getValue(), [
        'expires' => $cookie->getExpires(),
        'domain' => $cookie->getDomain(),
        'path' => $cookie->getPath(),
        'secure' => $cookie->isSecure(),
        'httpOnly' => $cookie->isHttpOnly(),
        'sameSite' => $cookie->getSameSite(),

Detailed description of using HttpSoft\Cookie\Cookie see here.

Detailed description of using HttpSoft\Cookie\CookieCreator see here.

Detailed description of using HttpSoft\Cookie\CookieManager see here.

Detailed description of using HttpSoft\Cookie\CookieSendMiddleware see here.