CakePHP: cookie reading issue

I am using the jQuery cookie plugin to set a cookie named "orderStatus" with a value of "success". This is a working find, and I checked, and the cookie is set correctly and present. However, when I come to read the cookie in my controller, for example:

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

and then the echo content of the contents of $ status is empty. Does anyone know what I'm doing wrong? I set the cake to use the cookie component, so this is not a problem. Thanks

+5
source share
5 answers

, , cookie, , cakephp. Cookie . read cookie, cookie .

cookie, javascript , $_COOKIE, PHP.

+12

, cookie, JavaScript, Cake Cookie Component . "CakeCookie"

, cookie :

CakeCookie[your_cookie_name]
+6

cookie? ?

<?php
 function testcookie() {
  $this->Cookie->write( 'test', 'somevalue' );
  echo $this->Cookie->read( 'test' );
 }
?>
+1

, . , cookie - , . , , .

, cookie, cookie CakePHP Model.field:

$this->Cookie->read('Order.status');

cookie , , .

0

jQuery CakePhp, , .

If you use, for example, jCookie ( https://github.com/carhartl/jquery-cookie ), the code below will work for you, replace myCompany with your cookie config name.

$.cookie.raw = true;
$.cookie('myCompany[cookie_name]', 'hallo', { expires: 365, path: '/'});

Then in your controller:

$this->Cookie->check('cookie_name') 

will return true (cakephp> 2.2) and

$this->Cookie->read('cookie_name') 

will return 'hallo'

When using cakephp 3.x it should look like this:

$this->Cookie->read('CakeCookie.cookie_name') 
0
source

All Articles