coercive / cookie
Coercive Security Cookie
2.0.0
2026-10-13 08:50 UTC
Requires
- php: >=7.4
- ext-openssl: *
- defuse/php-encryption: ^2.4
Requires (Dev)
- phpunit/phpunit: ^12
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Cookie allows you to create / read / delete normal or encrypted cookies.
Encryption is handled by defuse/php-encryption.
Compatible PHP 7.4 → 8.5
Get
composer require coercive/cookie
Usage
use Coercive\Security\Cookie\Cookie; # No need to reload the document. # The Cookie class creates and deletes cookies also in the super global $_COOKIE. # Instantiate and set your options $cookie = new Cookie; $cookie->setPath('/'); $cookie->setDomain('.domain.extension'); $cookie->setSecure(true); $cookie->setHttpOnly(true); $cookie->setSameSite(Cookie::SAMESITE_LAX); # Lax, Strict, None (None requires secure) # Any other value throws an InvalidArgumentException // etc... # Or all in the constructor $cookie = new Cookie('My Password', '/', '.domain.extension', true, true, Cookie::SAMESITE_LAX); # Option : anonymise -> cookie names are now sha1 + salt $cookie->anonymize(true, 'abcd1234'); # You can prefix anonymised cookie : Hello_************* $cookie->anonymize(true, 'abcd1234', 'Hello_'); # Plain cookie $cookie->set('MyCookie', 'Hey ! This is an example cookie !', time() + 600); $var = $cookie->get('MyCookie'); # Crypted cookie (needs a password : without it setSafe() returns false and getSafe() '') # A cookie that cannot be decrypted (wrong password, modified) is deleted and '' is returned. $cookie = new Cookie('My Password'); $cookie->setSafe('MyCookie', 'Hey ! This is an example cookie !', time() + 600); $var = $cookie->getSafe('MyCookie'); # Cookie exists if($cookie->has('MyCookie')) { // ... } # Delete cookie $cookie->delete('MyCookie'); # Disable / enable the whole system (all methods do nothing when disabled) $cookie->disable(); $cookie->enable();
Tests
composer install
vendor/bin/phpunit
PHPUnit 12 requires PHP 8.3+ to run the tests ; the library itself still runs on PHP 7.4.