Cookie Helper
The Cookie Helper provides a set of functions to simplify working with browser cookies in LavaLust. It allows you to set, retrieve, and delete cookies through a simple API.
Note
This helper uses the framework’s internal IO class through lava_instance()->io.
Make sure the IO class is properly loaded by the framework (it is loaded automatically in normal operation).
Function Summary
Function |
Description |
|---|---|
|
Sets a new cookie with optional parameters. |
|
Retrieves a cookie value from the browser. |
|
Deletes an existing cookie from the browser. |
Available Functions
- set_cookie($name, $value = '', $expiration = 0, $options = array())
Sets a cookie in the user’s browser.
- Parameters:
$name (
string) – The cookie name.$value (
string) – The cookie value (default: empty string).$expiration (
int) – Cookie expiration time in seconds (default: 0, meaning “until the browser is closed”).$options (
array) – Optional array of additional cookie settings such asdomain,path,secure, andhttponly.
- Returns:
void
Example:
<?php set_cookie('username', 'ronald', 3600, [ 'path' => '/', 'secure' => true, 'httponly' => true ])
- get_cookie($name)
Fetches an item from the
$_COOKIEarray.- Parameters:
$name (
string) – The name of the cookie to retrieve.
- Returns:
mixed — The cookie value if it exists, or
nullif it does not.
Example:
<?php $username = get_cookie('username'); if ($username !== null) { echo "Welcome back, $username!"; }
- delete_cookie($name, $domain = '', $path = '/', $prefix = '')
Deletes a cookie from the browser.
- Parameters:
$name (
string) – The name of the cookie to delete.$domain (
string) – The domain of the cookie (optional).$path (
string) – The path of the cookie (default:'/').$prefix (
string) – An optional cookie prefix.
- Returns:
void
Example:
<?php delete_cookie('username');
Additional Notes
If your app uses a cookie prefix (set via the
cookie_prefixconfig item),get_cookie()will attempt to resolve it automatically.The helper functions are simple wrappers around
lava_instance()->io->set_cookie()andlava_instance()->io->cookie()for convenience.