2012-06-14 307 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,但我会试试看,感谢您的链接。 –