Secure cookie handling with proper security attributes and optional encryption.
use Zappzarapp \Security \Cookie \SecureCookie ;
use Zappzarapp \Security \Cookie \CookieOptions ;
$ options = CookieOptions::strict ();
$ cookie = new SecureCookie ('session_id ' , 'abc123 ' , $ options );
// Send to browser
$ cookie ->send ();
Class
Description
SecureCookie
Secure cookie with proper attributes
CookieOptions
Configuration for cookie security attributes
Strict (Recommended for Sessions)
use Zappzarapp \Security \Cookie \CookieOptions ;
$ options = CookieOptions::strict ();
// HttpOnly: true
// Secure: true
// SameSite: Strict
// Path: /
Lax (For Navigation Cookies)
$ options = CookieOptions::lax ();
// SameSite: Lax - allows top-level navigation
$ options = new CookieOptions (
httpOnly: true , // Not accessible via JavaScript
secure: true , // HTTPS only
sameSite: 'Strict ' , // No cross-site requests
path: '/ ' , // Available site-wide
domain: '.example.com ' , // Available to subdomains
expires: time () + 3600 // 1 hour
);
Value
Description
Strict
Cookie never sent cross-site. Best for auth cookies.
Lax
Sent on top-level navigation (clicking links). Default in modern browsers.
None
Always sent (requires Secure). Use only when necessary for cross-site.
$ options = CookieOptions::strict ()
->withExpires (0 ); // Session cookie (expires when browser closes)
$ cookie = new SecureCookie ('PHPSESSID ' , session_id (), $ options );
$ cookie ->send ();
$ options = CookieOptions::strict ()
->withExpires (time () + 86400 * 30 ); // 30 days
$ cookie = new SecureCookie ('remember_me ' , $ token , $ options );
$ cookie ->send ();
$ cookie = new SecureCookie ('session_id ' , '' , CookieOptions::strict ())
->delete (); // Sets expiry in the past
Always use HttpOnly - Prevents XSS from stealing session cookies
Always use Secure - Prevents interception over HTTP
Prefer SameSite=Strict - Strongest CSRF protection
Avoid SameSite=None - Only if truly needed for cross-site functionality
Short expiry for sensitive cookies - Session cookies should expire with
the browser session
Limit Path scope - Use specific paths when cookie is only needed for
certain routes