PHP code example of paragonie / cookie

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

    

paragonie / cookie example snippets


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

$cookie = new \ParagonIE\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;
$cookie->save();

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

$cookie->delete();

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

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

// start session and have session cookie without any same-site restriction
\ParagonIE\Cookie\Session::start(null);

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

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

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

session_id();

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

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

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

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

   $value = \ParagonIE\Cookie\Session::take($key);
   $value = \ParagonIE\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 = \ParagonIE\Cookie\Cookie::parse($cookieHeader);