PHP code example of httpsoft / http-cookie

1. Go to this page and download the library: Download httpsoft/http-cookie library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

httpsoft / http-cookie example snippets


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

/**
 * @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
$manager->set($cookie1);
$manager->set($cookie2);
$manager->set($cookie3);

// 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
// ...