Get and set cookies in PHP with ease.
PHP >= 5.3
Install this package through Composer:
composer require overclokk/cookie
Autoload the vendor classes:
require_once 'vendor/autoload.php'; // Path may vary
And then use the Cookie
implementation:
$cookie = new \Overclokk\Cookie\Cookie();
or
$cookie = new \Overclokk\Cookie\Cookie( $_COOKIE );
This will return null
if the cookie doesn't exist or is expired.
$cookie_value = $cookie->get( 'cookie_name' );
If you don't specify $minutes
, a default of 0 will be used.
$minutes = 120 * 60;
$cookie->set( 'cookie_name', 'cookie_value', $minutes );
$cookie->forever( 'cookie_name', 'cookie_value' );
If the cookie doesn't exist, nothing will happen...
$cookie->delete('cookie_name');