2013-12-11 38 views
1

我正在爲需要密碼才能查看的特定頁面構建一個非常基本的認證系統。我發現其他幾個聽起來類似的問題,但只有那些有明確解決方案的問題涉及配置設置,這似乎不能解決我的問題。由於某種原因,$this->Session->write(...)總是返回false。CakePHP:Session-> write()不起作用

這裏是我的配置設置:

Configure::write('Session', array(
    'defaults' => 'php' 
)); 

此處,我試着寫在控制器動作會話:

private function _handle_auth_attempt($object) { 
    $submitted_pass = $this->request->data['Object']['password']; 
    $correct_pass = $object['Object']['password']; 
    $auth_cookie_name = $this->Object->auth_cookie_name($object); 

    debug($auth_cookie_name); //'Object1.pass' 
    debug($submitted_pass); //'foobar' 

    if (md5($submitted_pass) == md5($correct_pass)) { 
     $write1 = $this->Session->write($auth_cookie_name, md5($submitted_pass)); 
     $write2 = CakeSession::write($auth_cookie_name, md5($submitted_pass));    
     debug($write1); //FALSE 
     debug($write2); //FALSE 
     return TRUE; 
    } 

    $this->Session->setFlash('The password you entered is incorrect.'); 
    $this->redirect($this->referer()); 

} 

更新

裏面_handle_auth_attempt()我說:

$_SESSION['foo'] = 'bar'; 
$this->Session-read('foo'); //'bar' 

...他們工作得很好。所以我很確定這不是一個權限問題。

+1

你添加SessionComponent控制器? –

+0

你有清除瀏覽器緩存? – praveen

+1

@GuillemoMansilla我調試了$組件,並且在 - > write(...)之前看到陣列中的Session,Cookie和Auth。 – emersonthis

回答

1

從源代碼來看,FALSE的一種可能性是會話名稱爲空。 所以,你可以查閱一下結果是

debug($auth_cookie_name); 
+0

這是一個很好的建議,但我已經調試了所有變量,它們如預期的那樣。 – emersonthis

0

我不明白這一點在所有的,但我刪除了「」從我的會話cookie的名稱內,並導致Session->write()再次工作。

$this->Session->write('Object1.pass'); //FALSE 
$this->Session->write('Object1pass'); //TRUE 

雖然這解決了我的眼前的問題,我很想以更好地理解這個問題。

更新

後通過更多的思考這個問題,我懷疑是發生了什麼事是多種因素的組合:

  1. CakePHP中使用「點」符號來存儲這些我沒陣列內部會議不佔。
  2. 的第一次,我跑的代碼,它可能工作...
  3. 但它返回FALSE,因爲它已經存在<每個後續嘗試--untested
  4. 我也沒有解釋爲什麼read()被返回false。
0

所有會話的讀/寫屬於控制器:

$this->Session->write('User.still_login', 'Yes'); 

echo $this->Session->read('User.still_login'); // Yes as output