CakePHP Cookies

Cookies are the small files sent from a website and it stores the file inside the user system, while the user is browsing on the site. So, in this way, it contains all the information of the user to remember the user's activities or track the website status.

Cookie handling is easy and secure with CakePHP. A CookieComponent is a class used for managing the Cookies in web applications.

Configuration of Cookie

We have to configure Cookie in www\CakePHP3.8\src\Controller\AppController.php for globally access the files. The globally configured data will combine with the top-level configuration, so it will override those parts that are different. To set the global setting of cookies, use the config() method:

CakePHP Cookies

In this above picture, we have loaded the Cookie component and then used the config method like this.

 $this->Cookie->config('path', '/');
 $this->Cookie->;config([
 'expires' => '+10 days',
 'httpOnly' => false
 ]); 

To configure a specific key, use the configKey() method:

 $this->Cookie->configKey('Data', 'path', '/');
 $this->Cookie->configKey('User', [
  'expires' => '+1 days’,
  'httpOnly' => true
 ]); 

We can easily create a cookie with CookieComponent class for managing the cookie in CakePHP. By use of this class, we can perform several methods of cookie in CakePHP.

Write Cookie

We can write a cookie by using the write() method. Here is the syntax of the write method.

Cake\Controller\Component\CookieComponent::write(mixed $key, mixed $value = null)

 In this method, we have passed two arguments, Cookie variable ($key) and the value of the variable ($value).

Example

$this ->cookie -> write( ‘name’ ,’Amitabh’);

Here name is a key, and Amitabh is a value of the variable ‘name’.

And if we want to pass more than one key and its value then we can use an array in Cookies like:

 $this->Cookie->write('App', [ ‘name' => 'sachin',
  'age' => (int) 20,
  'tutorials' => 'CakePHP' ]); 

In the above syntax, App is an array that holds an array index with key such as name, age, and the tutorial. Also, we have declared the value of the key in a cookie.

Read Cookie

We can read cookie by use of read() method. Here is the syntax of read() method.

Cake\Controller\Component\CookieComponent::read(mixed
$key = null)

The read method contains one argument, either we can pass a single variable name or an array.

Example

 echo $this->Cookie->read (‘name’); // a single variable
 echo $this->Cookie->read (‘App); // here we passed app as an array 

Check Cookie

This Cookie method is used to check whether a Cookie key/path is present or not, and it has a null value in the cookie. We have defined the check method of Cookie:

Cake\Controller\Component\CookieComponent::check($key)

Example

echo
$this->Cookie ->check( ‘age’);

Delete Cookie

The delete method is used to remove the cookies from your system. We have defined the delete method here:

Cake\Controller\Component\CookieComponent::delete(mixed $key)

The delete method contains one argument either we can pass a single variable or an array to delete the cookie.

$this->Cookie->delete( ‘App’);

Example:

Create a controller name CookiesController.php file at the src/Controller/ folder of your project. Now write the following code in the controller file.

>?php
 namespaceApp\Controller;
 useApp\Controller\AppController;
 //useCake\Controller\Component\CookieComponent; // We have load cookie in //AppController
 classCookiesControllerextendsAppController{
 publicfunctionwriteCookie() {
 $this->Cookie->write('App',['name'=>'sachin',
 'age'=>(int)20,
 'tutorials'=>'CakePHP']);
 }
 publicfunctionreadCookie(){
 //$cookie_val=$this->Cookie->read('name');// for particular items
 $cookie_val=$this->Cookie->read('App'); // for array items
 $this->set( 'cookie_val ', $cookie_val);
 }
 publicfunctioncheckCookie(){
 $show=$this->Cookie->check ( 'App' );
 $this->set('show', $show);
 }
 publicfunctiondeleteCookie(){
 $this->Cookie->delete( 'App' );
 }
 }
 ?> 

Create a folder Cookies at src/Template and that folder you have to create a write_cookie.ctp file. Now write the following code in your file.

Cookiehas been saved.

Create a folder Cookies at src/Template and within that folder create a check_cookie.ctp file. Now write the following code in your file.

 >?php
 if($show):
 ?>
 Thecookieispresent.
 >?php
 else:
 ?>
 Thecookieisn'tpresent.
 >?php
 endif;
 ?> 

Create a folder Cookies at src/Template and in that folder, create a read_cookie.ctp file. Now write the following code in your file.

Thevalueofthecookieis:>?phpprint_r($cookie_val);?>

Create a folder Cookies at src/Template and, in that folder, create a delete_cookie.ctp file. Now write the following code in your file.

Cookiehasbeendeleted.

Now run your code in the localhost like localhost/CakePHP3.8/Cookies/write_cookie.ctp

Then it will produce this output:

CakePHP Cookies 1

Similarly, you can visit the given below url to get the output.

localhost/CakePHP3.8/cookies/ read_cookie.ctp

Then it will produce this output

CakePHP Cookies 2

Similarly, you can visit the given below url to get the output.

localhost/CakePHP3.8/cookies/ check_cookie.ctp

Then it will produce this output

CakePHP Cookies 3

Similarly, you can visit the given below url to get the output.

localhost/CakePHP3.8/cookies/delete_cookie.ctp

Then it will produce this output

CakePHP Cookies 4



ADVERTISEMENT
ADVERTISEMENT