Luminova Framework

PHP Luminova: Reading and Setting HTTP Cookies

Last updated: 2026-11-28 20:57:12

Set and read HTTP cookies with configurable expiration, path, domain, and security attributes.

Luminova's Cookie manager provides a convenient way to store data on the client-side, offering greater flexibility in managing cookie data compared to the SessionManager and CookieManager classes. With the Cookie class, you have the ability to configure the security access of the stored information according to your specific requirements.

The Cookie class stores data directly on the client-side, within cookies. This approach is particularly useful for scenarios where client-side storage is preferred or necessary, such as persisting user preferences, session tokens, or other application-specific data.


Usage

Write a preference cookie before sending any response body. An empty value allows you to create an instance to read a cookie already present in the request.

use Luminova\Cookies\Cookie;

$options = [
    'expires' => time() + 3600,
    'path' => '/',
    'httponly' => true,
    'samesite' => Cookie::LAX,
];

$theme = new Cookie('theme', 'dark', $options);
echo $theme->get(); // dark

In a later request, (new Cookie('theme'))->get() reads the incoming value. Use getValue() to read the value held by the instance.


Class Definition


Properties

options

The normalized options supplied at construction or through setOptions(). The table describes the framework defaults in Cookie::DEFAULT_OPTIONS.

/**
 * @var array<string, mixed>
 */
protected array $options = [];

Properties

NameTypeDescription
prefixstringCookie name prefix. Defaults to an empty string.
expiresintExpiration timestamp. Defaults to 0 for a session cookie.
pathstringCookie path. Defaults to /.
domainstringCookie domain. Defaults to an empty string.
secureboolRestricts transmission to HTTPS when enabled. Defaults to false.
httponlyboolPrevents script access when enabled. Defaults to true.
samesitestringSameSite policy. Defaults to Lax.
rawboolControls use of the raw cookie value. Defaults to false.

Constants

These constants define common cookie attributes and formatting options used for setting and managing cookies.

ConstantTypeValueDescription
NONEstring'none'Represents the SameSite=None policy, allowing cookies to be sent in all contexts, including cross-site requests.
LAXstring'lax'Represents the SameSite=Lax policy, allowing cookies in top-level navigations and some cross-site requests.
STRICTstring'strict'Represents the SameSite=Strict policy, restricting cookies to the same site only.

Methods

constructor

Create a cookie object. Passing a non-empty value updates the request-local cookie data and attempts to send a cookie header.

public final function __construct(string $name, mixed $value = '', CookieConfig|array $options = [])

Parameters:

ParameterTypeDescription
$namestringCookie name
$valuemixedcookie value (default '').
$optionsCookieConfig&#124;array<string,mixed>Options array or an instance of App\Config\Cookie. Omitting the argument uses application configuration. Include expires in a custom options array.

Throws:


newFromString

Create a new Cookie instance from a Set-Cookie header.

public static function newFromString(
    string $cookie,
    bool $raw = false,
    CookieConfig|array $options = []
): CookieInterface

Parameters:

ParameterTypeDescription
$cookiestringThe cookie header string.
$rawboolIndicates if the cookie is raw (default false).
$optionsCookieConfig&#124;array<string,mixed>Default cookie options or an instance of App\Config\Cookie (default []). Supply expires when using an options array.

Return:

CookieInterface - Return a new Cookie instance.


newFromArray

Create a cookie from an associative array or an object containing cookie data.

public static function newFromArray(
    array|object $cookies,
    CookieConfig|array $options = []
): CookieInterface

Parameters:

ParameterTypeDescription
$cookiesarray<string,mixed>&#124;objectCookie data with a name, a value, and any additional options.
$optionsCookieConfig&#124;array<string,mixed>Default cookie options or an instance of App\Config\Cookie (default []). Supply expires when using an options array.

Return:

CookieInterface - Return a new Cookie instance.


set

Set a cookie.

public function set(mixed $name, mixed $value, array $options = []): CookieInterface

Parameters:

ParameterTypeDescription
$namemixedThe name of the cookie.
$valuemixedThe value of the cookie.
$optionsarray<string,mixed>An array of cookie options.

Return:

CookieInterface - Return new Cookie instance.


setValue

Set the value of a cookie.

public function setValue(mixed $value): self

Parameters:

ParameterTypeDescription
$valuemixedThe value to set.

Return:

self - Return the instance of Cookie class.


setOptions

Set the options array used for subsequent writes. Set attributes at construction when you also need the attribute getters and toString() to reflect them.

public function setOptions(CookieConfig|array $options): self

Parameters:

ParameterTypeDescription
$optionsCookieConfig&#124;array<string,mixed>Options array or an instance of App\Config\Cookie. Include an expires entry when supplying an options array.

Return:

self - Return instance of Cookie class.


toString

Convert the cookie object to a string.

public function toString(): string

Return:

string - The string representation of the Cookie object.


toArray

Convert the cookie to an array.

public function toArray(): array

Return:

array<string,mixed> - Returns the normalized options and current cookie data.

KeyTypeDescription
namestringCookie name.
valuemixedCurrent instance value.
prefixstringCookie name prefix.
expiresintExpiration timestamp. Zero indicates a session cookie.
pathstringCookie path.
domainstringCookie domain.
secureboolHTTPS-only flag.
httponlyboolScript-access restriction.
samesitestringSameSite policy.
rawboolRaw-value flag.

getString

Alias for toString().

public function getString(): string

Return:

string - Returns the formatted cookie header value.


get

Retrieve the value of a cookie, if key is specified it will return value of the key .

public function get(?string $key = null): mixed

Parameters:

ParameterTypeDescription
$key?stringOptional key of the cookie to retrieve.

Return:

mixed - Returns the cookie value.


getName

Get the name of the cookie.

public function getName(): string

Return:

string - The name of the cookie.


getOptions

Get the options associated with the cookie.

public function getOptions(): array

Return:

array<string,mixed> - Returns normalized cookie options.

KeyTypeDescription
prefixstringCookie name prefix.
expiresintExpiration timestamp. Zero indicates a session cookie.
pathstringCookie path.
domainstringCookie domain.
secureboolHTTPS-only flag.
httponlyboolScript-access restriction.
samesitestringSameSite policy.
rawboolRaw-value flag.

getValue

Get the value of the cookie.

public function getValue(): mixed

Return:

mixed - The value of the cookie.


getDomain

Get the domain associated with the cookie.

public function getDomain(): string

Return:

string - The domain associated with the cookie.


getPrefix

Get the prefix associated with the cookie.

public function getPrefix(): string

Return:

string - The prefix associated with the cookie.


getMaxAge

Get the maximum age of the cookie.

public function getMaxAge(): int

Return:

int - The maximum age of the cookie.


getPath

Get the path associated with the cookie.

public function getPath(): string

Return:

string - The path associated with the cookie.


getSameSite

Get the SameSite attribute of the cookie.

public function getSameSite(): string

Return:

string - The SameSite attribute of the cookie.


getId

Get the ID of the cookie.

public function getId(): string

Return:

string - The ID of the cookie.


getPrefixedName

Get the prefixed name of the cookie.

public function getPrefixedName(): string

Return:

string - The prepended prefix name of the cookie.


getExpiry

Get the cookie expiration time.

public function getExpiry(bool $returnString = false): int|string

Parameters:

ParameterTypeDescription
$returnStringboolReturn cookie expiration timestamp or string presentation.

Return:

int|string - The expiration time.


isSecure

Check if the cookie is secure.

public function isSecure(): bool

Return:

bool - True if the cookie is secure, false otherwise.


isHttpOnly

Check if the cookie is HTTP-only.

public function isHttpOnly(): bool

Return:

bool - True if the cookie is HTTP-only, false otherwise.


isRaw

Check if the cookie value is raw.

public function isRaw(): bool

Return:

bool - True if the cookie value is raw, false otherwise.


isExpired

Check if the cookie has expired.

public function isExpired(): bool

Return:

bool - Return true if the cookie has expired otherwise false.


hasPrefix

Check if a cookie name has a prefix.

public function hasPrefix(?string $name = null): bool

Parameters:

ParameterTypeDescription
$name?stringThe name of the cookie.

Return:

bool - Returns true for a name starting with __Secure- or __Host-.


has

Check if a cookie exists, if key is passed NULL, it checks if the current cookie object name exists.

public function has(?string $key = null): bool

Parameters:

ParameterTypeDescription
$key?stringThe key of the cookie to check.

Return:

bool - True if the cookie exists, false otherwise.


delete

Remove a cookie, If key is null or empty, delete the entire cookie.

public function delete(?string $key = null): bool

Parameters:

ParameterTypeDescription
$key?stringThe key of the cookie to remove.

Return:

bool - Return true if the cookie was removed, false otherwise.


jsonSerialize

Provide the same array representation as __toArray() when encoding the object as JSON.

public function jsonSerialize(): mixed

Return:

array<array-key,mixed> - Returns the data passed to the JSON encoder.