2013-07-27 7 views
2

다음 코드를 사용하여 설정하고 CakePHP에서 쿠키를 읽습니다.CakePHP 쿠키가 설정되지 않음

public $components = array('Cookie'); 
public function beforeFilter(){ 
    parent::beforeFilter(); 
    $this->Cookie->name = 'saved_times'; 
    $this->Cookie->time = '1 year'; 
    $this->Cookie->domain = 'localhost'; 
    $this->Cookie->path = '/'; 
    $this->Cookie->httpOnly = false; 
    $this->Cookie->key = '+%)asG_~s*SAr&[email protected]@[email protected][email protected]*(XSL#$XOw!a232d#HKis~#^'; 
    $this->Cookie->secure = false; 

} 
public function save_position($time){ 
    if($this->Auth->user('id')){ 
     //save to the database 
     return true; 
    }else{ 
     //set browser cookie 
     $this->Cookie->write('time', $time); 
     echo "set", $this->Cookie->read('time'); 
    } 
} 

public function read(){ 
    echo $this->Cookie->read('time'), "test"; 
    print_r($this->Cookie->read('time')); 
} 

문제는 내가 볼 수있는 쿠키를 설정할 때, "설정"및 쿠키 값이 밖으로 반향 것입니다,하지만 난 단지 '테스트'없이 쿠키 값을보고 있어요 /read/를 방문 할 때. 또한 Chrome에서 쿠키 뷰어를 사용하지만 saved_times이 아닌 내 도메인에 대한 쿠키는 CAKEPHP입니다. 나는 쿠키가 설정되어 있다고 생각하지 않는다. 그러나 왜 그런지 모르겠다.

답변

4

보기가 렌더링 될 때까지 쿠키가 설정되지 않았다. 내 save_position 작업에 대한 조회가 없었으므로 오류 페이지가 표시되어 쿠키를 설정하지 않았습니다. save_position.ctp 파일을 추가하면 모든 작업이 정상적으로 처리됩니다.

+0

고맙습니다. 내 문제가 해결되었습니다. – romellem

관련 문제