how to read cookie value in cakephp view file

时光总嘲笑我的痴心妄想 提交于 2019-12-01 09:12:27

问题


in this i write the cookie value in controller file. i wanna read that cookie value in view file than how it possible.


回答1:


You must read it in the controller and set the value to make it available to the view:

$this->set('myValue', $this->Cookie->read('cookieValue'));

Then in the view, you can access the variable $myValue to return the value of 'cookieValue':

<?php echo $myValue; ?>



回答2:


Use Cookie components in AppController:

$components = array('Cookie');

Define following in AppController's beforeFilter():

$this->set('cookieHelper', $this->Cookie);

So that you could use it in view:

$cookieHelper->read('something');



回答3:


After all CakePHP is a PHP framework, you can read them by $_COOKIE :)

Bear in mind that you should use:

$this->Cookie->write('myValue', $value, false);

in your controller, because otherwise it will be encrypted and it will be hard to use :)




回答4:


I use the SessionComponent and SessionHelper to do this:

In the controller:

$this->Session->write('first_visit', true);

In the view:

if ($session->check('first_visit')) {
    $session->del('first_visit');
    echo $this->element('quick_intro');
}

You can also use $session->read('value') to read out a value from the session, instead of just checking if it exists.



来源:https://stackoverflow.com/questions/3264498/how-to-read-cookie-value-in-cakephp-view-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!