Luminova Framework

PHP Luminova: Examples of Client-Side Cookie Management

Last updated: 2024-08-28 22:34:36

Usages example for Luminova's client-side cookies.

Client-side cookie storage is a convenient way to store small pieces of data on the user's browser. This data persists even after the user closes the browser window, making it useful for various purposes such as session management, user preferences, and tracking. In this documentation we will show you examples on how you can use client-side cookie storage.


Examples

<?php 
use \Luminova\Cookies\Cookie;
$cookie = new Cookie('foo', 'bar');

Get protected properties of cookie object.

<?php 
echo $cookie->name . PHP_EOL;
echo $cookie->value . PHP_EOL;
echo $cookie->prefix . PHP_EOL;
echo $cookie->expires . PHP_EOL;
echo $cookie->path . PHP_EOL;
echo $cookie->domain . PHP_EOL;
echo $cookie->secure . PHP_EOL;
echo $cookie->httpOnly . PHP_EOL;
echo $cookie->sameSite . PHP_EOL;
echo $cookie->raw . PHP_EOL;
print_r($cookie->options);
print_r($cookie->default);

Check if cookie exists.

<?php
$cookie->has('foo') // True
$cookie->has('bar') // False

Check if cookie exists using the current object cookie name.

<?php
$cookie->has() // True

Accessing array cookie value by key index.

<?php 
use \Luminova\Cookies\Cookie;

$cookie = new Cookie('bar', ['key1' => 'Hello World', 'key2' => 'Luminova']);

Get value by key name.

<?php
echo $cookie->get('key2'); // Luminova

Get all the array values of the cookie.

<?php
print_r($cookie->get());
// Array ( [key1] => Hello World [key2] => Luminova )

Set cookie from cookie string.

<?php 
$str = 'theme=dark; Expires=Thu, 18 Dec 2025 12:00:00 UTC; Path=/; Domain=.localhost; secure; httponly; samesite=Lax';
$cookie = (new Cookie('bar'))->newFromString($str);

On this page