CakePHP – Cookie Management


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 CakeHttpCookieCookie;
use CakeHttpCookieCookieCollection;

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.

Syntax

The syntax for read cookie is as follows −

CakeHttpCookieCookieCollection::get($name)

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

CakeHttpCookieCookieCollection Interface::getValue()

Check Cookie

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

CakeHttpCookieCookieCollection::has($name)

Example

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

Delete Cookie

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

CakeHttpCookieCookieCollection::remove($name)

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.

config/routes.php

<?php
use CakeHttpMiddlewareCsrfProtectionMiddleware;
use CakeRoutingRouteDashedRoute;
use CakeRoutingRouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope(''/'', function (RouteBuilder $builder) {
   $builder->registerMiddleware(''csrf'', new CsrfProtectionMiddleware([
      ''httpOnly'' => true,
   ]));
   $builder->applyMiddleware(''csrf'');
   //$builder->connect(''/pages'',[''controller''=>''Pages'',''action''=>''display'', ''home'']);
   $builder->connect(''cookie/testcookies'',[''controller''=>''Cookies'',''action''=>''testCookies'']);
   $builder->fallbacks();
});

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

src/Controller/Cookies/CookiesController.php

<?php
   namespace AppController;
   use AppControllerAppController;
   use CakeHttpCookieCookie;
   use CakeHttpCookieCookieCollection;
   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'');
         $this->set(''cookie_val'',$cookie_val->getValue());
         $isPresent = $this->cookies->has(''name'');
         $this->set(''isPresent'',$isPresent);
         $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.

src/Template/Cookie/test_cookies.php

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

<br/>
<?php
   if($isPresent):
?>
The cookie is present.
<?php
   else:
?>
The cookie isn''t present.
<?php
   endif;
?>
<br/>
<?php
   echo "The count of cookie before delete is :" .$count;
?>
<br/>
<?php
   echo "The count of cookie after delete is :" .$count_afterdelete;
?>

Output

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

Cookie

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *