Why cookies are not saved in CakePHP?

It drives me crazy. In my AppController, I have the following:

public function beforeFilter() { $this->Cookie->name = 'MyCookie'; $this->Cookie->time = '1 year'; $this->Cookie->domain = 'http://mydomain.com'; $firstVisit = $this->Cookie->read('foo'); if ( empty($firstVisit) ) { $this->set('firstVisit', true); $this->Cookie->write('foo', 'true'); } else { $this->set('firstVisit', false); } } 

It seems like it should work, but nothing is returned and the cookie is completely empty.

What could prevent Cake from actually saving cookies?

+4
source share
2 answers

Cookies are not set until the visualization is displayed. Maybe you have no view for your controller?

+4
source

http:// made it break. Removal that fixes the problem.

+2
source

All Articles