Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp: Reading cookie problem

I am using the jQuery cookie plugin to set a cookie called 'orderStatus' with a value of 'success'. This is working find and I have checked and the cookie is set correctly and present. However when I come to read the cookie in my controller like so:

$status = $this->Cookie->read('orderStatus');

and then echo the contents of $status its empty. Anyone know what I'm doing wrong? I've set cake to use the cookie component so thats not the problem. Thanks

like image 928
geoffs3310 Avatar asked Dec 08 '10 14:12

geoffs3310


2 Answers

The answer is, unfortunately, you can not read a cookie that was written by any other method using cakephp. The Cookie component in cake was not written with interoperability in mind. The read method can only read cookies that were written by the cake cookie component methods.

In order to read a cookie set by javascript in your controller, you have to access the $_COOKIE variable native to PHP.

like image 124
Neil S Avatar answered Sep 27 '22 21:09

Neil S


It will work if you use the same namespace for your cookie saved by JavaScript that is used by Cake Cookie Component as default. The namespace is 'CakeCookie'

So your cookie must look something like:

CakeCookie[your_cookie_name]
like image 29
kriskodzi Avatar answered Sep 27 '22 21:09

kriskodzi