Luminova Framework

PHP Luminova: Client Side Cookie Management

Last updated: 2024-08-28 22:33:25

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.



Properties

The default cookie options.

    protected array $default = [
        'prefix' => '',
        'expires'  => 0,
        'path'     => '/',
        'domain'   => '',
        'secure'   => false,
        'httponly' => true,
        'samesite' => 'Lax',
        'raw'      => false,
    ];

Constants

ConstantVisibilityTypeValue
NONEpublic'none'
LAXpublic'lax'
STRICTpublic'strict'
EXPIRES_FORMATpublic'D, d-M-Y H:i:s T'

Methods

constructor

Cookie class initialization. This class cannot be extended.

new Cookie(string $name, mixed $value = '', array $options = []): mixed

Parameters:

ParameterTypeDescription
$namestringCookie name
$valuemixedcookie value
$optionsarrayOptional cookie options if not passed the default will be used

Throws:


newFromString

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

public newFromString(string $cookie, bool $raw = false): \Luminova\Interface\CookieInterface

Parameters:

ParameterTypeDescription
$cookiestringThe cookie header string.
$rawboolIndicates if the cookie is raw.

Return Value:

CookieInterface - A new Cookie instance.


set

Set a cookie.

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

Parameters:

ParameterTypeDescription
$namemixedThe name of the cookie.
$valuemixedThe value of the cookie.
$optionsarrayAn array of cookie options.

Return Value:

CookieInterface - Return new Cookie instance.


setValue

Set the value of a cookie.

public setValue(mixed $value): \Luminova\Interface\CookieInterface

Parameters:

ParameterTypeDescription
$valuemixedThe value to set.

Return Value:

CookieInterface - The Cookie instance.


get

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

public get(string|null $key = null): mixed

Parameters:

ParameterTypeDescription
$keystring|nullOptional key of the cookie to retrieve.

Return Value:

mixed - The value of the cookie.


has

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

public has(string|null $key = null): bool

Parameters:

ParameterTypeDescription
$keystring|nullThe 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|null $key = null): \Luminova\Interface\CookieInterface

Parameters:

ParameterTypeDescription
$keystring|nullThe key of the cookie to remove.

Return Value:

CookieInterface - This Cookie instance.


setOptions

Set cookie options.

public setOptions(string|array $options): \Luminova\Interface\CookieInterface

Parameters:

ParameterTypeDescription
$optionsstring|arrayAn array of cookie options or a class name.

Return Value:

CookieInterface - This Cookie instance.


hasPrefix

Check if a cookie name has a prefix.

public hasPrefix(string|null $name = null): bool

Parameters:

ParameterTypeDescription
$namestring|nullThe name of the cookie.

Return Value:

bool - True if the cookie name has a prefix, false otherwise.


toString

Convert the cookie object to a string.

public toString(): string

Return 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.


getName

Get the name of the cookie.

public getName(): string

Return Value:

string - The name of the cookie.


getOptions

Get the options associated with the cookie.

public getOptions(): array

Return Value:

array - The options associated with the cookie.


getValue

Get the value of the cookie.

public getValue(): mixed

Return Value:

mixed - The value of the cookie.


getDomain

Get the domain associated with the cookie.

public getDomain(): string

Return Value:

string - The domain associated with the cookie.


getPrefix

Get the prefix associated with the cookie.

public getPrefix(): string

Return Value:

string - The prefix associated with the cookie.


getMaxAge

Get the maximum age of the cookie.

public getMaxAge(): int

Return Value:

int - The maximum age of the cookie.


getPath

Get the path associated with the cookie.

public getPath(): string

Return Value:

string - The path associated with the cookie.


getSameSite

Get the SameSite attribute of the cookie.

public getSameSite(): string

Return Value:

string - The SameSite attribute of the cookie.


isSecure

Check if the cookie is secure.

public isSecure(): bool

Return Value:

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


isHttpOnly

Check if the cookie is HTTP-only.

public isHttpOnly(): bool

Return Value:

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


isRaw

Check if the cookie value is raw.

public isRaw(): bool

Return Value:

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


getId

Get the ID of the cookie.

public getId(): string

Return Value:

string - The ID of the cookie.


getPrefixedName

Get the prefixed name of the cookie.

public getPrefixedName(): string

Return Value:

string - The prepended prefix name of the cookie.


getExpiry

Get the cookie expiration time.

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

Parameters:

ParameterTypeDescription
$return_stringboolReturn cookie expiration timestamp or string presentation.

Return Value:

string|int - The expiration time.


hasExpired

Check if the cookie has expired.

public hasExpired(): bool

Return Value:

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