PHP code example of delight-im / cookie

1. Go to this page and download the library: Download delight-im/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/ */

    

delight-im / cookie example snippets


     

\Delight\Cookie\Cookie::setcookie('SID', '31d4d96e407aad42');
// or
\Delight\Cookie\Cookie::setcookie('SID', '31d4d96e407aad42', time() + 3600, '/~rasmus/', 'example.com', true, true, 'Lax');

$cookie = new \Delight\Cookie\Cookie('SID');
$cookie->setValue('31d4d96e407aad42');
$cookie->setMaxAge(60 * 60 * 24);
// $cookie->setExpiryTime(time() + 60 * 60 * 24);
$cookie->setPath('/~rasmus/');
$cookie->setDomain('example.com');
$cookie->setHttpOnly(true);
$cookie->setSecureOnly(true);
$cookie->setSameSiteRestriction('Strict');

// echo $cookie;
// or
$cookie->save();
// or
// $cookie->saveAndSet();

(new \Delight\Cookie\Cookie('SID'))->setValue('31d4d96e407aad42')->setMaxAge(60 * 60 * 24)->setSameSiteRestriction('None')->save();

$cookie->delete();
// or
$cookie->deleteAndUnset();

   \Delight\Cookie\Cookie::exists('first_visit');
   

   \Delight\Cookie\Cookie::get('first_visit');
   // or
   \Delight\Cookie\Cookie::get('first_visit', \time());
   

// start session and have session cookie with 'lax' same-site restriction
\Delight\Cookie\Session::start();
// or
\Delight\Cookie\Session::start('Lax');

// start session and have session cookie with 'strict' same-site restriction
\Delight\Cookie\Session::start('Strict');

// start session and have session cookie without any same-site restriction
\Delight\Cookie\Session::start(null);
// or
\Delight\Cookie\Session::start('None'); // Chrome 80+

session_regenerate_id();
// and
session_regenerate_id(true);

\Delight\Cookie\Session::regenerate();
// and
\Delight\Cookie\Session::regenerate(true);

\Delight\Cookie\Session::id();

session_id();

   $value = \Delight\Cookie\Session::get($key);
   // or
   $value = \Delight\Cookie\Session::get($key, $defaultValue);
   

   \Delight\Cookie\Session::set($key, $value);
   

   if (\Delight\Cookie\Session::has($key)) {
       // ...
   }
   

   \Delight\Cookie\Session::delete($key);
   

   $value = \Delight\Cookie\Session::take($key);
   $value = \Delight\Cookie\Session::take($key, $defaultValue);
   

$cookieHeader = 'Set-Cookie: test=php.net; expires=Thu, 09-Jun-2016 16:30:32 GMT; Max-Age=3600; path=/~rasmus/; secure';
$cookieInstance = \Delight\Cookie\Cookie::parse($cookieHeader);