PHP code example of omegaalfa / cookie

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

    

omegaalfa / cookie example snippets


use omegaalfa\Cookie;

// Set a cookie
Cookie::make('username', 'John Doe')
     ->withExpiration(3600)
     ->withPath('/')
     ->withDomain('.example.com')
     ->withSecure(true)
     ->withHttpOnly(true)
     ->send();

// Get the value of a cookie
$username = Cookie::get('username');

// Delete a cookie
Cookie::delete('username');

## Métodos

### `set`
Define um novo cookie. Aceita vários parâmetros para configurar o cookie, incluindo nome, valor, tempo de expiração, caminho, domínio, se deve ser seguro, se deve ser acessível apenas via HTTP, e o atributo `SameSite`.

Cookie::get('theme', 'default');

Cookie::delete('theme', '/', 'example.com', true);

if (Cookie::exists('theme')) { // O cookie existe }

if (Cookie::isSecure('theme')) { // O cookie é seguro }

if (Cookie::isHttpOnly('theme')) { // O cookie é HTTP-only }

$expirationTime = Cookie::getExpirationTime('theme');

$domain = Cookie::getDomain('theme');

$path = Cookie::getPath('theme');

$allCookies = Cookie::getAllCookies();

Cookie::clearAllCookies();

if (Cookie::checkCookieConsent()) { // O usuário deu consentimento }

$matches = Cookie::getCookieValueByRegex('/^theme/');

Cookie::deleteCookiesByRegex('/^theme/');