Cakephp 2 многомерный массив cookie не работает - PullRequest
0 голосов
/ 31 октября 2018

Я не могу прочитать файл cookie, созданный с использованием компонента cookie CakePHP. На самом деле куки создаются, и когда я отлаживаю сразу после метода записи, они отображают все куки. Но если я обновлю ту же страницу (действие), куки не читаются. На других действиях контроллера также куки не читаются.

var $components = array('Encryption','General','Cookie');

public function beforeFilter()
{
    parent::beforeFilter();
    $this->Auth->allow();   

    $this->Cookie->name =null;
    $this->Cookie->time =2592000;  // or '30 days'
    $this->Cookie->path = '/';
    $this->Cookie->domain =  $_SERVER['HTTP_HOST'];
    //$this->Cookie->secure = true;  // i.e. only sent if using secure HTTPS
    $this->Cookie->key = md5(uniqid(rand(), true));
    //$this->Cookie->httpOnly = true;
    $this->Cookie->type('aes');
}
public function index(){    
    $browserInfo=$this->General->getBrowser();
    $browserName=$browserInfo['name'];
    $cookie_value=md5(uniqid(rand(), true))."_".$browserName;

    $knowTheBae= $this->Cookie->read('know_the_bae'); // this is returning null when i' refresh the page again. 

    if(count($knowTheBae)==0){  
        $this->Cookie->write('know_the_bae.identity',$cookie_value); //this line suppose to execute only first time. But it's executing every time i load this page .
        debug($this->Cookie->read('know_the_bae')); //line no 43
    }
    if($this->Cookie->read('know_the_bae.identity')!=""){
        if($this->Session->read('Auth.User.id')){
            $this->Cookie->write('know_the_bae.user.name',$this->Session->read('Auth.User.username'));
            $this->Cookie->write('know_the_bae.user.id',$this->Session->read('Auth.User.id'));
        }else{
            $this->Cookie->write('know_the_bae.user.name','UNKN_USR');
            $this->Cookie->write('know_the_bae.user', array('name' => 'UNKN_USR', 'role' => 'guest'));

        }
    }else{
        $this->Cookie->write('know_the_bae.identity',$cookie_value);
    }
    $this->Cookie->write("know_the_bae.test",'something going wrong');

    debug($this->Cookie->read('know_the_bae')); //line no 59
}

вывод cookie браузера

1 Ответ

0 голосов
/ 01 ноября 2018

Вы используете случайный ключ для шифрования и дешифрования ваших файлов cookie:

$this->Cookie->key = md5(uniqid(rand(), true));

Это будет другим при следующей загрузке страницы, поэтому он не сможет расшифровать то, что вы установили. Вы всегда должны использовать один и тот же ключ, я бы посоветовал вам сохранить его в защищенном файле конфигурации или в переменной среды.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...