Download the PHP package activecollab/cookies without Composer
On this page you can find all versions of the php package activecollab/cookies. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download activecollab/cookies
More information about activecollab/cookies
Files in activecollab/cookies
Package cookies
Short Description Easily get and set cookies
License MIT
Homepage https://labs.activecollab.com
Informations about the package cookies
activecollab/cookies
A thin PSR-7 cookie library built on top of dflydev/fig-cookies. It adds automatic name prefixing, optional value encryption, URL-based configuration, and secure defaults so you can work with cookies without worrying about the low-level header plumbing.
Installation
Quick Start
Every method accepts and returns standard PSR-7 message objects (ServerRequestInterface, ResponseInterface), so the library slots into any middleware stack.
How It Works
The library revolves around a single entry point, the Cookies class (implementing CookiesInterface). It is responsible for three operations:
Getting a cookie reads the Cookie header from the incoming request and returns the value for the given name (after optional decryption and prefix resolution).
Setting a cookie applies the value to both the request object (so downstream middleware can see it immediately) and the response object (by adding a Set-Cookie header with all configured attributes like domain, path, expiry, security flags, etc.).
Removing a cookie works like setting operation, but with an empty value and an expiry date in the past, so the browser discards it.
Configuration
All configuration is done through a fluent interface on the Cookies instance. Every setter returns $this, so calls can be chained.
Settings Overview
| Setting | Getter | Setter | Default | Description |
|---|---|---|---|---|
| Domain | getDomain() |
domain(string) |
'' (empty) |
Cookie domain attribute. |
| Path | getPath() |
path(string) |
'/' |
Cookie path attribute. |
| Secure | getSecure() |
secure(bool) |
true |
Send cookie only over HTTPS. |
| HttpOnly | getHttpOnly() |
httpOnly(bool) |
true |
Hide cookie from JavaScript. |
| SameSite | getSameSite() |
sameSite(SameSite) |
Lax |
Cross-site request policy. |
| Default TTL | getDefaultTtl() |
defaultTtl(int) |
1209600 (14 days) |
Time-to-live in seconds. |
| Prefix | getPrefix() |
prefix(string) |
'' (empty) |
String prepended to every cookie name. |
| Encryptor | getEncryptor() |
encryptor(EncryptorInterface) |
null |
Encrypt/decrypt cookie values. |
Secure, HttpOnly, and the SameSite
The library ships with modern security defaults out of the box:
Override any of them when the use case requires it:
Per-Cookie Settings
The set() method accepts an optional $settings array as its fifth argument. These options override global defaults for that single cookie only:
| Key | Type | Default | Description |
|---|---|---|---|
ttl |
int |
Value of getDefaultTtl() (14 days) |
Time-to-live in seconds. The cookie's Expires attribute is computed as current timestamp plus this value. |
http_only |
bool |
Value of getHttpOnly() (true) |
Whether the cookie should be hidden from JavaScript. Set to false for cookies that client-side code needs to read. |
same_site |
SameSite |
Value of getSameSite() (Lax) |
Cross-site request policy for this cookie. Accepts SameSite::lax(), SameSite::strict(), or SameSite::none(). |
encrypt |
bool |
true |
Whether to encrypt the value before writing. Only relevant when an encryptor is configured. Set to false to store the value in plain text. |
All other cookie attributes (domain, path, and secure) are always taken from the global Cookies configuration and cannot be overridden per cookie.
Example — a short-lived, JS-accessible, unencrypted preference cookie:
Example — a CSRF token cookie with a stricter same-site policy than the global default:
Configure From URL
The most convenient way to configure domain, path, secure, and prefix all at once is from your application's URL:
An http:// URL will set secure to false. The prefix is only auto-generated when one has not already been set, so calling prefix() before configureFromUrl() preserves your choice.
Cookie Name Prefixing
Prefixing avoids collisions when multiple applications share the same domain:
Encryption
When an encryptor is provided, cookie values are automatically encrypted on set() and decrypted on get(). The encryptor must implement ActiveCollab\Encryptor\EncryptorInterface from the activecollab/utils package.
To skip encryption for a specific cookie, pass 'encrypt' => false in settings:
TTL and Expiry
The default time-to-live is 14 days. Change it globally or per cookie:
All versions of cookies with dependencies
ext-openssl Version *
activecollab/utils Version ^3.0
dflydev/fig-cookies Version ^3.0
psr/http-server-middleware Version ^1.0