Giter VIP home page Giter VIP logo

cakephp-cookies's Introduction

CakePHP Cookies Component

CookiesComponent is a replacement for default and depricated CookieComponent from CakePHP. The CookiesComponent is a wrapper around the native PHP setcookie() method. It makes it easier to manipulate cookies, and encrypt or decrypt cookie data.

Install Cookies

  • Copy the src > Controller > Component > CookiesComponent.php to your Component folder.
  • Create Utility folder in src if doesn't exists.
  • Copy the src > Utility > CookiesEncryptionTrait.php to your Utility folder.
  • Load the component in the controller with $this->loadComponent('Cookies'); command.

Requirements

  • PHP >= 7.1.x
  • CakePHP >= 3.6.x

Cookie name

  • Simple name: mycookie
  • Array dot notation: mycookie.othercookie

Encrypt and Decrypt

Cookie value can be encrypted and decrypted. By default both values are false.

function read(string $name, bool $decrypt = false): mixed
function write(string $name, mixed $value, bool $encrypt = false): void

Use encrypt parameter. Write a cookie with encrypted data value.

$this->Cookies
    ->setConfig('expire', '+1 day')
    ->write('mycookie', time(), true);

Use decrypt parameter. Read an encrypted cookie data value.

echo $this->Cookies->read('mycookie', true);

Remove

Remove a cookie value from the cookie array data value, but will not delete the cookie.

$this->Cookies->remove('mycookie.othercookie');

Delete

Delete the cookie completely.

$this->Cookies->delete('mycookie');

Example

Load Cookies component and create a cookie, check if exists and read the cookie value.

namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;

/**
 * @package App\Controller
 * @property \App\Controller\Component\CookiesComponent $Cookies
 */
class AppController extends Controller
{
    // Your code
    public function initialize()
    {
        $this->loadComponent('Cookies');
    }

    /**
     * @inheritdoc
     * @param Event $event Event
     * @return \Cake\Http\Response|null|void
     */
    public function beforeFilter(Event $event)
    {
        // Global settings
        $this->Cookies->setConfig('expire', 0);
        $this->Cookies->setConfig('path', '/');
        $this->Cookies->setConfig('secure', false);
    }

    public function index()
    {
        // Get Cookies config object
        $this->set('Cookies', $this->Cookies->getConfig(null, true));

        // Write a cookie
        $this->Cookies
            ->setConfig('expire', '+1 day')
            ->write('mycookie', time());

        // Check if the cookie exists
        $cookieExists = $this->Cookies->check('mycookie');
        if ($cookieExists) {
            // Read cookie value
            echo $this->Cookies->read('mycookie');
        }
    }
    // Your code
}

Enjoy ;)

cakephp-cookies's People

Contributors

mrred85 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

carlose119

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.