Search by

coercive / cookie

Coercive

Coercive Security Cookie

Package info

github.com/Coercive/Cookie

Homepage

pkg:composer/coercive/cookie

Statistics

Installs: 1 564

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

2.0.0 2026-10-13 08:50 UTC

This package is auto-updated.

Last update: 2026-10-13 09:04:09 UTC


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.