2012-06-14 303 views
0

我有一個簡單的cakephp應用程序,我使用本教程Simple Authentication and Authorization Application構建。我有和本教程中相同的代碼塊。CakePHP登錄失敗

我在我的電腦上使用Apache/MySQL運行它,它一切正常。當我將它部署到我的共享主機(Bluehost)時,我開始獲取登錄失敗。

我檢查了我的本地和服務器上的MySQL數據庫。區別在於我的本地數據庫中的密碼被哈希,但主機數據庫中的密碼很簡單。 (這是相同的確切代碼運行。)(我想清楚這一點,這不是我的意圖,顯然是一種不同的行爲,但我不知道它是什麼原因。)

這是我的表:

CREATE TABLE users (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    username VARCHAR(50), 
    password VARCHAR(50), 
    role VARCHAR(20), 
    created DATETIME DEFAULT NULL, 
    modified DATETIME DEFAULT NULL 
); 

這些是從我的控制器相關的方法爲註冊和登錄分別爲:

public function add() { 
    if ($this->request->is('post')) { 
     $this->User->create(); 
     if ($this->User->save($this->request->data)) { 
      $this->Session->setFlash(__('The user has been saved')); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
     } 
    } 
} 

public function login() { 
    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      $this->redirect($this->Auth->redirect()); 
     } else { 
      $this->Session->setFlash(__('Invalid username or password, try again')); 
     } 
    } 
} 

,最後,這是我的模型的beforeSave()方法:

public function beforeSave() { 
    if (isset($this->data[$this->alias]['password'])) { 
     $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); 
    } 
    return true; 
} 

在我的本地計算機上,我可以先添加一個用戶,然後用該用戶登錄。在服務器上,我可以添加一個用戶,但是當我想我登錄看到錯誤消息「無效的用戶名或密碼,請重試」

+0

你爲什麼不散列存儲在服務器上的密碼? – gcochard

+0

這就是我所要求的。我的意圖是存儲他們散列。出於某種原因,它們以平淡的形式存儲。我不知道是什麼導致了這種行爲差異。 –

+0

好像'AuthComponent ::密碼($這 - >數據[$這 - >別名] [ '密碼']);'呼叫失敗。 – gcochard

回答

2

看起來你需要定義beforeSave函數接受默認參數。這似乎是CakePHP的和PHP 5.4

public function beforeSave($options = array()) { 
    if (isset($this->data[$this->alias]['password'])) { 
     $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); 
    } 
    return true; 
} 

之間的問題在這裏看到更多的信息:
http://community.webfaction.com/questions/8397/cakephp-2-auth-failing-on-production-only

+0

生產我的PHP版本是5.2.17,但我會試試看,感謝您的鏈接。 –