rafaeldms/cookie

A simple component way to work with Cookies

1.1.1 2022-12-08 22:14 UTC

This package is auto-updated.

Last update: 2024-11-09 02:41:27 UTC


README

Maintainer Source Code PHP from Packagist Latest Version Software License Quality Score Total Downloads

The Cookie is a component for creating Cookies in the browser with the possibility to create, read, edit and remove cookies.

O Cookie é um componente para criação de Cookies nos navegadores tendo a possibilidade de criar, ler, editar e remover cookies.

Highlights

  • Simple Installation (Instalação simples)
  • Easy get instance (Fácil de instanciar)
  • Total controle of the cookies (Controle total dos cookies)
  • Create cookies with base_64 encryption (Crie cookies com criptografia base_64)
  • Composer ready and PSR-2 compliant (Pronto para o composer e Compatível com PSR-2)

Installation

Cookie is available via Composer:

"rafaeldms/cookie": "^1.*"

or run

composer require rafaeldms/cookie

Documentation

For details on how to use the Cookie, see the sample folder with details in the component directory

Para mais detalhes sobre como usar o Cookie, veja a pasta de exemplo com detalhes no diretório do componente

To start using the Cookie you can Instantiate a new Class or use it in an abstract way

Para começar a usar o Cookie pode Instanciar uma nova Classe ou utilizando métodos estáticos

Create Cookie using static methods (Criando cookies usando o método estático)

use RafaelDms\Cookie\StaticCookie;

/**
 * Create a new cookie using the static method
 */
 StaticCookie::setCookie::set("test", "new_test", 10);

Get Cookie using static methods (Obter Cookie usando métodos estáticos)

 /**
 * get value the static method
 */
 echo StaticCookie::get("test");

 echo "<br><br>";

Create values as array (Criando valores em array)

/**
 * Create value as array
 */
StaticCookie::set('user', ['name' => 'Rafael', 'role' => "Developer"], 10);

Get Cookie using static methods (Obter Cookie usando métodos estáticos)

/**
 * get value as array the static method
 */
echo StaticCookie::get('user')['role'];


echo "<br><br>";

Delete cookie using static method (Deletar cookie usando método estático)

/**
 * remove the static class
 */
StaticCookie::destroy('test');

Create if it doens't exist using method static (Criando um cookie se não existir usando método estático)

/**
 * create if it doesn't exist the static method
 */
StaticCookie::setDoesntHave('testIfDoesntHave', true, 10000);

Create and delete if it exists the static method (Criando e deletando um cookie se existir usando método estático)

/**
 * create if it doesn't exist the static method
 */
StaticCookie::setDoesntHave('testIfDoesntHave', 'ok', 12500, "/admin", true);

Check if exists using a static method (Verificando se um cookie existe usando método estático)

/**
 * check if exists cookie the static class
 */
if (StaticCookie::has('testIfDoesntHave')) {
    echo "Cookie testIfDoesntHave exist";
} else {
    echo "Cookie testIfDoesntHave not exist";
}

echo "<br><br>";

Check if exists by value using a static method (Verificando se um cookie existe um valor usando método estático)

/**
 * check if exists by value
 */

if (StaticCookie::has("testIfDoesntHave", 1)) {
    echo "the value is equal to ok";
} else {
    echo "the vlaue is no equal to ok";
}
echo "<br><br>";

Instance a new cookie object (Instanciando um novo objeto cookie)

use RafaelDms\Cookie\Cookie;

/**
 * Create a new cookie using construct method
 */
$cookie = new Cookie("testCookie");

Set methods

Set value

/**
 * set value a cookie using methot set
 */
$cookie->setValue("123456");

Set expiryTime

/**
 * set expiryTime
 */
$cookie->setExpiryTime(24 * 60 * 60);

Set path

/**
 * set path
 */
$cookie->setPath("/admin");

Set domain

/**
 * set domain
 */
$cookie->setDomain("https://www.cagep.com.br");

Set http only

/**
 * set httpOnly to false
 */
$cookie->setHttpOnly(false);

Set secure only

/**
 * set secure only to true
 */
$cookie->setSecureOnly(true);

Save and set

/**
 * save a cookies
 */
$cookie->saveAndSet();

var_dump($cookie);

Get methods

Get value

/**
 * get value
 */
echo "Value: " . $cookie->getValue();
echo "<br><br>";

Get expire time

/**
 * get expire time
 */
echo "Expire Time: " . $cookie->getExpiryTime();
echo "<br><br>";

Get path

/**
 * get path
 */
echo "Path: " .  $cookie->getPath();
echo "<br><br>";

Get domain

/**
 * get domain
 */
echo "Domain: " .  $cookie->getDomain();
echo "<br><br>";

Is http only

/**
 * is http only
 */
echo "Http only: " . ($cookie->isHttpOnly() ? 'true' : 'false');
echo "<br><br>";

Is secure only

/**
 * is secure only
 */
echo "Secure only: " . ($cookie->isSecureOnly() ? 'true' : "false");
echo "<br><br>";

Update value (set new value)

/**
 * update value
 */
$cookie->setValue("910111213");

var_dump($cookie);

Check if exists cookie and delete cookie

/*
 * check if exists cookie and delete cookie
 */
if($cookie->hasCookie()){
    var_dump($cookie);
}

Delete and unset cookie

/**
 * delele cookie and unset
 */
$cookie->deleteAndUnset();

var_dump($cookie);

Contributing

Please see CONTRIBUTING for details.

Support

Security: If you discover any security related issues, please email rafael@cagep.com.br instead of using the issue tracker.

Se você descobrir algum problema relacionado à segurança, envie um e-mail para rafael@cagep.com.br em vez de usar o rastreador de problemas.

Thank you

Credits

License

The MIT License (MIT). Please see License File for more information.