how to read cookie value in cakephp view file

前端 未结 4 1878
执笔经年
执笔经年 2021-01-15 15:40

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

相关标签:
4条回答
  • 2021-01-15 15:49

    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 :)

    0 讨论(0)
  • 2021-01-15 15:52

    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');
    
    0 讨论(0)
  • 2021-01-15 15:56

    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; ?>
    
    0 讨论(0)
  • 2021-01-15 16:03

    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.

    0 讨论(0)
提交回复
热议问题