PHP code example of elephpant / cookie

1. Go to this page and download the library: Download elephpant/cookie library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elephpant / cookie example snippets



use ElePHPant\Cookie\Cookie\Cookie;

// default expiration: minutes
// default encryption: there is no encryption
$cookie = new Cookie();

// change expiration unit
$options = [
    'expiration' => 'days', // seconds, minutes, hours, days, weeks, months, years
];

// set Base64 encryption
$options = [
    'encryption' => \ElePHPant\Cookie\Strategies\Encryption\Base64EncryptionStrategy::class,
];

// set AES-256 encryption
$options = [
    'encryption' => \ElePHPant\Cookie\Strategies\Encryption\AES256EncryptionStrategy::class,
    'encrypt_key' => 'SET_YOUR_ENCRYPT_KEY_HERE', // 

$str = 'john_doe';
$arr = ['name' => 'John Doe', 'email' => '[email protected]', 'age' => 30,];

// name, value(s), expiration, ...
$cookie::set('username', $str, 20);
$cookie::set('user', $arr, 20);

echo $cookie::get('username'); // john_doe


$arr = $cookie::get('user');
var_export($arr); // array ( 'name' => 'John Doe', 'email' => '[email protected]', 'age' => 30, )
echo $arr['email']; // [email protected]


$all = $cookie::get();
var_export($all); // array ( 'username' => 'john_doe', 'user' => array ( 'name' => 'John Doe', 'email' => '[email protected]', 'age' => 30, ), )


var_export($_COOKIE); // array ( 'username' => '9sV1OIHc7taGoafeXWjl+gcrJFpIpg8Hkqe4fdGRygI=', 'user' => 'rLrCW9eBvoPijA+bSuIIrqbWccbYJqk2aPK5RGMwiLNpMZw2nYrrU7A2Zmuk3CGt0XiXlXpcQQv7h40M/6jbYslrlsvTJXm3mtG0nyiRDCg=', )

$cookie::setDoesntHave('cookie_consent', true, 60);

$cookie::setDoesntHave('toggle_sidebar', true, 60, true);

$cookie::destroy('user');
$cookie::destroy(); // all

if ($cookie::has('food')) {
    echo 'The cookie exists.';
} else {
    echo 'The cookie does not exist.';
}

if ($cookie::has('username', $str)) {
    echo 'The cookie exists with the correct value.';
} else {
    echo 'The cookie does not exist or has a different value.';
}

if ($cookie::has('user', $arr)) {
    echo 'The cookie exists with the correct value.';
} else {
    echo 'The cookie does not exist or has a different value.';
}