Setting New Cookies on CakePHP

I am currently modifying an existing CakePhp project and I would like to set a cookie on a specific project page (it has a .ctp file), so first I tried to put the setcookie php code at the beginning of the .ctp template (before the doctype tag, as I did would be on a regular php website), but it does not work, cookie is not set.

So my question is: how can I add a cookie to the visitor when I get to page X (which its template file is called list.ctp (for example)) on the website?

Thanks everyone!

+3
source share
3 answers
  • set cookie to component variable in controller

    var $components = array('Cookie');

  • In any action you can write a cookie with

    $this->Cookie->write('anyname', cookieData, $encrypt = false, $expires = null);

  • To read the saved cookie, just call this code

    $this->Cookie->read('anyname');

+9

You must set a cookie in the controller for any kind / page that you want to set in a cookie. So, if the .ctp file is in the users directory, then you should set the cookie in user_controller.php in the function named as your .ctp file: users / index.ctp, add the cookie to the / users _controller.php controllers in the index function.

0
source

Source: https://habr.com/ru/post/1794843/


All Articles