PHP Luminova: Cookie Management in Client-Side
The cookie class enhanced client-side data storage without Default JavaScript or HTTP-only restrictions, allowing you to configure your restrictions as needed.
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.
Class Definition
- Class namespace:
\Luminova\Cookies\Cookie - This class implements:Luminova\Interface\CookieInterface, Luminova\Interface\LazyInterface, \Stringable
Properties
The default cookie options.
protected array $default = [
'prefix' => '',
'expires' => 0,
'path' => '/',
'domain' => '',
'secure' => false,
'httponly' => true,
'samesite' => 'Lax',
'raw' => false,
];Constants
These constants define common cookie attributes and formatting options used for setting and managing cookies.
| Constant | Type | Value | Description |
|---|---|---|---|
NONE | string | 'none' | Represents the SameSite=None policy, allowing cookies to be sent in all contexts, including cross-site requests. |
LAX | string | 'lax' | Represents the SameSite=Lax policy, allowing cookies in top-level navigations and some cross-site requests. |
STRICT | string | 'strict' | Represents the SameSite=Strict policy, restricting cookies to the same site only. |
Methods
constructor
Cookie class initialization. This class cannot be extended.
new Cookie(string $name, mixed $value = '', array $options = []): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | Cookie name |
$value | mixed | cookie value |
$options | array | Optional cookie options if not passed the default will be used |
Throws:
- Luminova\Exceptions\CookieException - Throws cookie exception.
newFromString
Create a new Cookie instance from a Set-Cookie header.
public newFromString(string $cookie, bool $raw = false, Cookie|array $options = []): \Luminova\Interface\CookieInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$cookie | string | The cookie header string. |
$raw | bool | Indicates if the cookie is raw. |
$options | App\Config\Cookie|array<string,mixed> | An array of default cookie options or cookie config class object. |
Return Value:
Luminova\Interface\CookieInterface - Return a new Cookie instance.
newFromArray
Create a new Cookie instance from an array or json object.
public newFromArray(array|object $cookies, Cookie|array $options = []): \Luminova\Interface\CookieInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$cookie | array<string,mixed>|object | An associative array or json object of cookies. |
$options | App\Config\Cookie|array<string,mixed> | An array of default cookie options or cookie config class object. |
Return Value:
Luminova\Interface\CookieInterface - Return a new Cookie instance.
set
Set a cookie.
public set(mixed $name, mixed $value, array $options = []): \Luminova\Interface\CookieInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The name of the cookie. |
$value | mixed | The value of the cookie. |
$options | array | An array of cookie options. |
Return Value:
CookieInterface - Return new Cookie instance.
setValue
Set the value of a cookie.
public setValue(mixed $value): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$value | mixed | The value to set. |
Return Value:
self - Return the instance of Cookie class.
setOptions
Set cookie options.
public setOptions(Cookie|array $options): \Luminova\Interface\CookieInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$options | App\Config\Cooke|array<string,mixed> | An array of cookie options or cookie config class object. |
Return Value:
CookieInterface - Return instance of Cookie class.
toString
Convert the cookie object to a string.
public toString(): stringReturn Value:
string - The string representation of the Cookie object.
toArray
Convert the cookie to an array.
public toArray(): array<string, mixed>Return Value:
array<string, mixed> - The array representation of the Cookie object.
get
Retrieve the value of a cookie, if key is specified it will return value of the key .
public get(?string $key = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string|null | Optional key of the cookie to retrieve. |
Return Value:
mixed - The value of the cookie.
getName
Get the name of the cookie.
public getName(): stringReturn Value:
string - The name of the cookie.
getOptions
Get the options associated with the cookie.
public getOptions(): arrayReturn Value:
array - The options associated with the cookie.
getValue
Get the value of the cookie.
public getValue(): mixedReturn Value:
mixed - The value of the cookie.
getDomain
Get the domain associated with the cookie.
public getDomain(): stringReturn Value:
string - The domain associated with the cookie.
getPrefix
Get the prefix associated with the cookie.
public getPrefix(): stringReturn Value:
string - The prefix associated with the cookie.
getMaxAge
Get the maximum age of the cookie.
public getMaxAge(): intReturn Value:
int - The maximum age of the cookie.
getPath
Get the path associated with the cookie.
public getPath(): stringReturn Value:
string - The path associated with the cookie.
getSameSite
Get the SameSite attribute of the cookie.
public getSameSite(): stringReturn Value:
string - The SameSite attribute of the cookie.
getId
Get the ID of the cookie.
public getId(): stringReturn Value:
string - The ID of the cookie.
getPrefixedName
Get the prefixed name of the cookie.
public getPrefixedName(): stringReturn Value:
string - The prepended prefix name of the cookie.
getExpiry
Get the cookie expiration time.
public getExpiry(bool $return_string = false): string|intParameters:
| Parameter | Type | Description |
|---|---|---|
$return_string | bool | Return cookie expiration timestamp or string presentation. |
Return Value:
string|int - The expiration time.
isSecure
Check if the cookie is secure.
public isSecure(): boolReturn Value:
bool - True if the cookie is secure, false otherwise.
isHttpOnly
Check if the cookie is HTTP-only.
public isHttpOnly(): boolReturn Value:
bool - True if the cookie is HTTP-only, false otherwise.
isRaw
Check if the cookie value is raw.
public isRaw(): boolReturn Value:
bool - True if the cookie value is raw, false otherwise.
isExpired
Check if the cookie has expired.
public isExpired(): boolReturn Value:
bool - Return true if the cookie has expired otherwise false.
hasPrefix
Check if a cookie name has a prefix.
public hasPrefix(?string $name = null): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string|null | The name of the cookie. |
Return Value:
bool - True if the cookie name has a prefix, false otherwise.
has
Check if a cookie exists, if key is passed NULL, it checks if the current cookie object name exists.
public has(?string $key = null): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string|null | The key of the cookie to check. |
Return Value:
bool - True if the cookie exists, false otherwise.
delete
Remove a cookie, If key is null or empty, delete the entire cookie.
public delete(?string $key = null): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string|null | The key of the cookie to remove. |
Return Value:
bool - Return true if the cookie was removed, false otherwise.