Skip to content
Muhammet Şafak edited this page Jun 10, 2026 · 2 revisions

InitPHP Cookies — Wiki

Welcome to the official documentation for initphp/cookies — a signed, tamper-evident cookie manager for PHP. Your values live in a single browser cookie whose payload is authenticated with an HMAC-SHA256 signature, so a client can read it but cannot forge or alter it. Each value can carry its own time-to-live, and writes are staged in memory and flushed with one call.

The package ships three types:

Type Purpose
Cookie The default concrete implementation (final).
CookieInterface The contract — type-hint this in your services.
CookieInvalidArgumentException Thrown on an invalid name/salt, value, or TTL.
composer require initphp/cookies
use InitPHP\Cookies\Cookie;

// The salt is the HMAC secret — keep it private and stable across requests.
$cookie = new Cookie('app_session', getenv('COOKIE_SALT'));

$cookie->set('user_id', 42);
$cookie->set('flash', 'Saved!', 60); // expires in 60 seconds

$cookie->send(); // write the staged changes before any output

On the next request:

$cookie = new Cookie('app_session', getenv('COOKIE_SALT'));

$cookie->has('user_id');      // true
$cookie->get('user_id');      // 42 (int — scalar types are preserved)
$cookie->pull('flash');       // reads the value once, then removes it
$cookie->get('missing', '-'); // '-' (default)

Start here

At a glance — capabilities

Capability Supported
HMAC-SHA256 signed payload
Constant-time verification (hash_equals)
Object-injection-safe deserialization (allowed_classes => false)
Tamper → discard & re-issue clean
Per-key time-to-live
Many values under one named cookie
Deferred writes (stage in memory, one send())
Scalar type preservation (int/float/bool/string)
Injectable cookie source & writer (for tests)
Confidentiality / encryption of values ❌ (integrity only — see Security Model)

Package metadata

Integrity, not secrecy. Signing proves a cookie was issued by you and has not been altered; it does not hide the values from the client. Never store secrets in a cookie — see the Security Model.

If something in this wiki is unclear, ambiguous, or wrong, please open an issue — documentation fixes are reviewed eagerly.

Clone this wiki locally