PHP code example of siteparts / http-message-cookies

1. Go to this page and download the library: Download siteparts/http-message-cookies 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/ */

    

siteparts / http-message-cookies example snippets


use DateTime;
use SiteParts\Cookies\HttpMessage\ResponseCookieTemplate;

$cookieTemplate = new ResponseCookieTemplate([
	'domain' => 'example.org',
	'path' => '/foo',
	'expires' => new DateTime("+14 days"),
]);

// $response is instance of Psr\Http\Message\ResponseInterface

$response = $cookieTemplate
	->create("lang", "en")
	->addTo($response);

$response = $cookieTemplate
	->create("color", "blue")
	->withSecure(true)
	->addTo($response);

use DateTime;
use SiteParts\Cookies\HttpMessage\ResponseCookie;

// $response is instance of Psr\Http\Message\ResponseInterface

$response = new ResponseCookie("lang", "en")
	->withDomain("example.org")
	->withPath("/foo")
	->withExpires(new DateTime("+14 days"))
	->addTo($response);

$response = new ResponseCookie("color", "blue")
	->withDomain("example.org")
	->withPath("/foo")
	->withExpires(new DateTime("+14 days"))
	->withSecure(true)
	->addTo($response);

$cookieTemplate = new ResponseCookieTemplate([
	'domain' => 'example.org',
	'path' => '/foo',
	'expires' => new DateTime("+14 days"),
	'max_age' => 14 * 24 * 60 * 60,
	'secure' => true,
	'http_only' => true,
	'same_site' => 'Strict',
]);

$cookie = new ResponseCookie("lang", "en")
	->withDomain("example.org")
	->withPath("/foo")
	->withExpires(new DateTime("+14 days"))
	->withMaxAge(14 * 24 * 60 * 60)
	->withSecure(true)
	->withHttpOnly(true)
	->withSameSite("Strict");