我試圖設置一個登錄頁面,但是當我嘗試登錄時,即使使用了錯誤的用戶名/密碼,cake重定向到登錄頁面(註銷功能正確重定向) 。即使我插入了錯誤的信息,我也沒有發現任何錯誤,我不明白。這裏是我的控制器代碼:cakephp登錄頁面沒有去任何地方
class UsersController extends AppController {
public $name='Users';
public $layout='pagelayout';
public $uses=array('User');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add', 'logout', 'overview');
}
public function login() {
$this->set('title', 'Log in to your Gulf Shores 4 Less account');
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
}
,這裏是我的模型:
<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $name='User';
public $hasMany=array('Unit', 'Complex', 'Coupon', 'Location', 'Image', 'Charter', 'Course', 'Nightclub', 'Store');
public function beforeSave() {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'advertiser')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
)
);
}
?>
下面是從AppController中的代碼:
<?php
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'overview'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'index')
)
);
function beforeFilter() {
$this->Auth->allow('login','index', 'view', 'condos', 'houses', 'hotels_and_motels', 'print_all_coupons', 'print_coupon', 'search', 'golf', 'charters', 'events', 'nightlife', 'shopping', 'visitors_info', 'contact_us');
}
}
?>
而這裏查看代碼:
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php echo __('Please enter your username and password'); ?></legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login'));?>
</div>
正如您所看到的,我幾乎複製並粘貼了Cakephp-2.0手冊中的內容。我的數據庫表和手冊的唯一區別是我的密碼在我的用戶表中存儲爲MD5散列。我無法弄清楚這是哪裏出軌了。
** **更新我改變了我的login()函數,現在給了我一個登錄信息不正確時出現錯誤信息。我認爲現在的問題是,因爲我的密碼存儲在帶有MD5散列的數據庫中,所以它不檢查正確的密碼。我如何告訴Cake使用MD5哈希檢查器? – huzzah 2012-02-13 19:47:11
**再次更新**。我讀過蛋糕中的默認散列是sha1,所以我將其中一個密碼更改爲sha1散列,但仍保留在登錄頁面(它仍然告訴我不正確的用戶名/密碼) – huzzah 2012-02-13 19:59:02