1. Go to this page and download the library: Download tobento/service-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/ */
tobento / service-cookie example snippets
use Tobento\Service\Cookie;
use Tobento\Service\Encryption\EncrypterInterface;
// Set up cookie values for reading cookies:
$cookieValues = new Cookie\CookieValues($_COOKIE);
// You may use the default processor for decryption:
$processor = new Cookie\CookiesProcessor(
encrypter: null, // null|EncrypterInterface
whitelistedCookies: ['PHPSESSID'],
);
$cookieValues = $processor->processCookieValues($cookieValues);
// Start reading values:
$value = $cookieValues->get('foo');
// Set up Cookies for writing:
$cookies = new Cookie\Cookies(
cookieFactory: new Cookie\CookieFactory(),
);
// Adding cookies:
$cookies->add('name', 'value');
// You may use the default processor for encryption:
$cookies = $processor->processCookies($cookies);
// Send cookies before any header is sent:
foreach($cookies as $cookie) {
$cookie->send();
}
use Psr\Http\Server\MiddlewareInterface;
use Tobento\Service\Cookie\Middleware;
use Tobento\Service\Cookie\CookieValuesFactory;
use Tobento\Service\Cookie\CookiesFactory;
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\CookiesProcessor;
use Tobento\Service\Cookie\CookieValuesFactoryInterface;
use Tobento\Service\Cookie\CookiesFactoryInterface;
use Tobento\Service\Cookie\CookiesProcessorInterface;
$middleware = new Middleware\Cookies(
// CookieValuesFactoryInterface
cookieValuesFactory: new CookieValuesFactory(),
// CookiesFactoryInterface
cookiesFactory: new CookiesFactory(new CookieFactory()),
// CookiesProcessorInterface
cookiesProcessor: new CookiesProcessor(),
);
var_dump($middleware instanceof MiddlewareInterface);
// bool(true)
use Psr\Http\Message\ServerRequestInterface;
use Tobento\Service\Cookie\CookieValuesInterface;
use Tobento\Service\Cookie\CookiesInterface;
// ...
public function index(ServerRequestInterface $request): void
{
// read cookies:
$cookieValues = $request->getAttribute(CookieValuesInterface::class);
$value = $cookieValues->get('foo');
// or
var_dump($request->getCookieParams());
// write cookies:
$cookies = $request->getAttribute(CookiesInterface::class);
$cookies->add('name', 'value');
}
use Tobento\Service\Cookie\CookieValuesFactory;
use Tobento\Service\Cookie\CookieValuesFactoryInterface;
use Tobento\Service\Cookie\CookieValuesInterface;
$factory = new CookieValuesFactory();
var_dump($factory instanceof CookieValuesFactoryInterface);
// bool(true)
$cookieValues = $factory->createCookieValuesFromArray($_COOKIE);
var_dump($cookieValues instanceof CookieValuesInterface);
// bool(true)
use Tobento\Service\Cookie\CookieValuesFactory;
use Tobento\Service\Cookie\CookiesInterface;
$factory = new CookieValuesFactory();
$cookieValues = $factory->createCookieValuesFromCookies(
$cookies // CookiesInterface
);
use Tobento\Service\Cookie\CookieValues;
use Tobento\Service\Cookie\CookieValuesInterface;
$cookieValues = new CookieValues(['name' => 'value']);
var_dump($cookieValues instanceof CookieValuesInterface);
// bool(true)
use Tobento\Service\Cookie\CookieValues;
$values = new CookieValues([
'name' => 'value',
'meta' => [
'color' => 'red',
],
]);
var_dump($values->get(name: 'name'));
// string(5) "value"
// supports array access:
var_dump($values['name']);
// supports dot notation:
var_dump($values->get(name: 'meta.color'));
// string(3) "red"
// with a default if not exists:
var_dump($values->get(name: 'foo', default: 'value'));
// string(5) "value"
var_dump($values->get(name: 'foo'));
// NULL
use Tobento\Service\Cookie\CookieValues;
$values = new CookieValues([
'name' => 'value',
]);
$valuesNew = $values->withValues(['name' => 'value']);
use Tobento\Service\Cookie\CookiesFactory;
use Tobento\Service\Cookie\CookiesFactoryInterface;
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\CookiesInterface;
use Tobento\Service\Cookie\CookieInterface;
use Tobento\Service\Cookie\Cookie;
$cookiesFactory = new CookiesFactory(
cookieFactory: new CookieFactory(),
);
var_dump($cookiesFactory instanceof CookiesFactoryInterface);
// bool(true)
$cookies = $cookiesFactory->createCookies();
var_dump($cookies instanceof CookiesInterface);
// bool(true)
$cookies = $cookiesFactory->createCookies(
new Cookie(name: 'name', value: 'value'),
);
use Tobento\Service\Cookie\CookiesFactory;
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\CookiesInterface;
$cookiesFactory = new CookiesFactory(
cookieFactory: new CookieFactory(),
);
$cookies = $cookiesFactory->createCookiesFromKeyValuePairs([
'name' => 'value',
]);
var_dump($cookies instanceof CookiesInterface);
// bool(true)
use Tobento\Service\Cookie\CookiesFactory;
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\CookiesInterface;
$cookiesFactory = new CookiesFactory(
cookieFactory: new CookieFactory(),
);
$cookies = $cookiesFactory->createCookiesFromArray([
[
'name' => 'name',
'value' => 'value',
// The duration in seconds until the cookie will expire.
'lifetime' => 3600,
'path' => '/',
'domain' => '.example.com',
'secure' => true,
'httpOnly' => true,
'sameSite' => 'Lax',
],
]);
var_dump($cookies instanceof CookiesInterface);
// bool(true)
use Tobento\Service\Cookie\Cookies;
use Tobento\Service\Cookie\CookiesInterface;
use Tobento\Service\Cookie\CookieFactory;
$cookies = new Cookies(
cookieFactory: new CookieFactory(),
);
var_dump($cookies instanceof CookiesInterface);
// bool(true)
use Tobento\Service\Cookie\Cookies;
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\Cookie;
$cookies = new Cookies(
cookieFactory: new CookieFactory(),
);
$cookies->addCookie(
new Cookie(name: 'name', value: 'value')
);
use Tobento\Service\Cookie\Cookies;
use Tobento\Service\Cookie\CookieFactory;
$cookies = new Cookies(
cookieFactory: new CookieFactory(),
);
$cookies->add(
name: 'name', // string
value: 'value', // string
// The duration in seconds until the cookie will expire.
lifetime: 3600, // null|int
// if null (default) it uses default value from factory.
path: '/', // null|string
// if null (default) it uses default value from factory.
domain: 'example.com', // null|string
// if null (default) it uses default value from factory.
secure: true, // null|bool
httpOnly: true, // default true if not set
// if null (default) it uses default value from factory.
sameSite: 'Lax', // string
);
$cookie = $cookies->get(name: 'name');
// by name and path:
$cookie = $cookies->get(name: 'name', path: 'path');
// by name and domain:
$cookie = $cookies->get(name: 'name', domain: 'example.com');
// by name, path and domain:
$cookie = $cookies->get(name: 'name', path: 'path', domain: 'example.com');
// clears all with the same name:
$cookies->clear(name: 'name');
// clears only with same name and path:
$cookies->clear(name: 'name', path: 'path');
// clears only with same name and domain:
$cookies->clear(name: 'name', domain: 'example.com');
// clears only with same name, path and domain:
$cookies->clear(name: 'name', path: 'path', domain: 'example.com');