2014-04-16 26 views
0

我想在用戶登錄時寫入cookie,並希望下次用戶打開該頁面時,它讀取cookie名稱並在登錄前顯示。 foloowing是我使用的代碼: - 視圖如何在cakephp中讀取cookie 2

var $components = array('Cookie'); 

function beforeFilter() 
{ 
    $this->Cookie->name = 'LOGIN_COOKIE'; 
    $this->Cookie->time = time()+60*60*24*30; //cookie expire after 30 days 
    $this->Cookie->path = '/'; 
    $this->Cookie->domain = 'example.com/'; 
    $this->Cookie->secure = false; 
    $this->Cookie->key = '39lbkutg1i2l0kta6785d8qki5'; 
    $this->Cookie->httpOnly = true; 
} 

在登錄功能

$this->Cookie->write('COOKIE_USER', array(
         'name' => $UserDetails['SystemUser']['Name'], 
         'email' => $UserDetails['SystemUser']['email'], 
         'lastname' => $UserDetails['SystemUser']['LastName'] 
)); 

$readCookie=$this->Cookie->read('COOKIE_USER'); 

,但它給錯誤 「缺少幫手」

 Error: CookieHelper could not be found. 
    Error: Create the class CookieHelper below in file: app/View/Helper/CookieHelper.php 

請指導我。

+0

這個代碼?您使用Cookie組件,但嘗試使用Cookie助手閱讀而不加載它...... –

+0

如果用戶之前訪問過我們的網站,我需要在視圖中顯示名稱和電子郵件。 所以我必須閱讀cookie。你知道我該如何顯示名稱和電子郵件? –

+0

是的,我知道,看下面的答案... –

回答

-1

要對控制器使用 -

$this->Cookie->read() 

在View讀取Cookie在你的控制器使用 -

CookieComponent::read(); 
1

讀取Cookie做到這一點

//Write enter code here 
$this->set('readCookie', $this->Cookie->write('COOKIE_USER'=> array(
         'name' => $UserDetails['SystemUser']['Name'], 
         'email' => $UserDetails['SystemUser']['email'], 
         'lastname' => $UserDetails['SystemUser']['LastName'] 
))); 

//Read 
$this->set('readCookie', $this->Cookie->read('COOKIE_USER')); 


//write into a key 
$this->Cookie->write('COOKIE_USER.name'=>'Piya'); 
//Read a key 
$this->set('username', $this->Cookie->read('COOKIE_USER.name')); 

那麼在你看來

debug($readCookie); 
echo $username; 

評論這些,如果你爲什麼要上查看閱讀的cookie,你不需要他們

//function beforeFilter() 
//{ 
// $this->Cookie->name = 'LOGIN_COOKIE'; 
// $this->Cookie->time = time()+60*60*24*30; //cookie expire after 30 days 
// $this->Cookie->path = '/'; 
// $this->Cookie->domain = 'example.com/'; 
// $this->Cookie->secure = false; 
// $this->Cookie->key = '39lbkutg1i2l0kta6785d8qki5'; 
// $this->Cookie->httpOnly = true; 
//} 

look at this example

+0

我已添加註釋,但在登錄時顯示messgae - : - 無法修改標題信息 - 標題已發送 –

+0

@PiyaSharma據我所知,不應該有任何輸出直到視圖被渲染。檢查報告的文件和行以尋找奇怪的東西。 –

+0

我沒有卡住這個錯誤,但是問題的關鍵是,它是我的安全鹽表示,但實際上,用cipherSeed工作的數字來改變! ! 我已經解決了它.. –

0

嘗試在控制器

if ($auth) { 
       $this->Auth->setUser($auth); 
       if ($this->request->data['remember'] == 1) 
        $this->Cookie->write('User', $this->request->data); 
       else 
        $this->Cookie->delete('User'); 
       return $this->redirect($this->Auth->redirectUrl()); 
      } 
      $this->Flash->set(__('Invalid credentials.'), ['element' => 'default', 'params' => ['class' => 'alert alert-danger']]); 
     } 
     if ($this->Cookie->check('User')) { 
      $this->request->data['email'] = $this->Cookie->read('User.email'); 
      $this->request->data['password'] = $this->Cookie->read('User.password'); 
      $this->request->data['remember'] = $this->Cookie->read('User.remember'); 
     }