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
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With