PHP code example of drewlabs / cookie

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

    

drewlabs / cookie example snippets




use Drewlabs\Cookie\Factory;

// Creates a session cookie with name and value as first parameters
$cookie = Factory::new()->create('sessionId',uniqid('session').time(), 0);

use Drewlabs\Cookie\Factory;

$expiresAt = (new \DateTimeImmutable())->format('U');
$cookie = Factory::new()->createFromString("sessionId=e8bb43229de9; Expires=$expiresAt; Domain=foo.example.com; Path=/; Secure; HttpOnly");

  
  $cookie = Factory::new()->create('id', "oldCookieValue");

  $cookie2 = $cookie->withValue("newCookieValue");

  echo $cookie->getValue(); // oldCookieValue
  echo $cookie2->getValue(); //newCookieValue
  

  
  $cookie = Factory::new()->create('id', uniqid('cookie').time());
  $cookie2 = $cookie->withDomain("com.azlabs.xyz");

  echo $cookie->getDomain(); // null
  echo $cookie2->getDomain(); //com.azlabs.xyz
  

  
  $cookie = Factory::new()->create('id', uniqid('cookie').time());
  $cookie2 = $cookie->withPath("/auth");

  echo $cookie->getPath(); // /
  echo $cookie2->getPath(); // /auth
  

  $cookie = Factory::new()->create('id', uniqid('cookie').time());
  $cookie2 = $cookie->withHttpOnly();
  

  
  $cookie = Factory::new()->create('id', uniqid('cookie').time());
  $cookie2 = $cookie->withSecure();