CakePHP - Cookie Management


Handling Cookie with CakePHP is easy and secure. There is a CookieComponent class which is used for managing Cookie. The class provides several methods for working with Cookies.

To work with cookies, add this 2 classes to your controller −

use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;

The cookie object has to be created first to register a cookie.

$cookie = new Cookie(name,value,expiration time,path,domain);

The name and value are mandatory and others are optional param.

Write Cookie

Following is the syntax to write a cookie.

$cookie = new Cookie(name,value,expiration time,path,domain);

The cookie created has to be added to cookieCollection as shown below −

$cookie = new Cookie('name','XYZ');
$cookies = new CookieCollection([$cookie]);

If the cookie collection object is already created, the rest of the cookies can be added as shown below −

$cookies = $cookies->add($cookie);

Read Cookie

To read cookie make use of get() method from cookiecollection.


The syntax for read cookie is as follows −


This will return you cookiecollection Interface, to get the value of the cookie, you will have to call the method getValue().

Cake\Http\Cookie\CookieCollection Interface::getValue()

Check Cookie

The has() method from cookieCollection will tell you, if the cookie is present or not.



echo $isPresent = $this->cookies->has('name');

Delete Cookie

The remove() method is used to delete cookie. Following is the syntax of the remove() method.


The remove() method will take one argument, the name of cookie variable ($name) to delete.

Example 1

$test = $this->cookies->remove('name');

Example 2

Make changes in the config/routes.php file as shown in the following program.


use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->scope('/', function (RouteBuilder $builder) {
   $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
      'httpOnly' => true,
   //$builder->connect('/pages',['controller'=>'Pages','action'=>'display', 'home']);

Create a CookiesController.php file at src/Controller/CookiesController.php. Copy the following code in the controller file.


   namespace App\Controller;
   use App\Controller\AppController;
   use Cake\Http\Cookie\Cookie;
   use Cake\Http\Cookie\CookieCollection;
   class CookiesController extends AppController{
      public $cookies;
      public function testCookies() {
         $cookie = new Cookie('name','XYZ');
         $this->cookies = new CookieCollection([$cookie]);
         $cookie_val = $this->cookies->get('name');
         $isPresent = $this->cookies->has('name');
         $this->set('count', $this->cookies->count());
         $test = $this->cookies->remove('name');
         $this->set('count_afterdelete', $test->count());

Create a directory Cookies at src/Template and under that directory create a View file called test_cookies.php. Copy the following code in that file.


The value of the cookie is: <?php echo $cookie_val; ?>

The cookie is present.
The cookie isn't present.
   echo "The count of cookie before delete is :" .$count;
   echo "The count of cookie after delete is :" .$count_afterdelete;


Execute the above example by visiting the following URL − http://localhost/cakephp4/cookie/testcookies