我知道在这个问题上有很多问题,但是我已经尝试添加所有分辨率,但到目前为止仍然没有运气,所以我分享我的代码,看看是否有人可以给我一个解决方案。感谢。
我已成功删除了密码,我已在密码数据库中添加了文本字段,因此我对字符数量没有任何问题。但每次我尝试登录时,都会显示无效的用户名/密码,并且不会重定向或其他任何内容。我是编程和PHP的新手。请帮忙。
这是我的代码:
AppController.php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $helpers = array('Html', 'Session', 'Form' );
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth'=>array(
'loginRedirect'=>array('Controller'=>'user', 'action'=>'index'),
'logoutRedirect'=>array('Controller'=>'user', 'action'=>'index'),
'authError'=>"you are not allowed to access that page",
'authorize'=>array('Controller')
)
);
public function isAuthorized($user){
return TRUE;
}
public function beforeFilter() {
$this->Auth->allow('index', 'add');
}
}
login.ctp
<h1> Login Form</h1>
<?php
echo $this->Form->create('User');
echo $this->Form->input('user_name');
echo $this->Form->input('password');
echo $this->Form->end(__('Login'));
?>
user.php的
// hash password before saving It
/*
public function beforeSave($options = array()) {
$this->data['User']['password'] =
AuthComponent::password($this->data['User']['password']);
return TRUE;
}
*/
public function beforeSave($options = array()) {
if (isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User'] ['password']);
}
return TRUE;
}
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'user_id';
/**
* Display field
*
* @var string
*/
public $displayField = 'user_name';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
//USERNAME VALIDATION
'username' => array(
'required' => array(
'rule' => array('minLength', 1),
'allowEmpty' => false,
'message' => 'Please enter a title.'
)
),
'user_name' => array(
'required' => array(
'rule' => array( 'isUnique' ),
'message' => 'Username already exist. Please try again',
//'allowEmpty' => false,
//'required' => TRUE,
//'last' => TRUE, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
//EMAIL ADDRESS VALIDATION
'email_address' => array(
'required' => array(
'rule' => array('minLength', 1),
'allowEmpty' => false,
'message' => 'Please add an email'
)
),
'email_address' => array(
'required' => array(
'rule' => array( 'isUnique' ),
'message' => 'Email already exist in our database. Please try again',
//'allowEmpty' => false,
//'required' => TRUE,
//'last' => TRUE, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'password'=>array(
'not empty' => array(
'rule'=>'notEmpty',
'Message'=>'Password is empty'
),
'Match Passwords'=> array(
'rule'=>'matchPasswords',
'message'=>'Password do not match'
)
),
'password_confirmation'=>array(
'not empty' => array(
'rule'=>'notEmpty',
'Message'=>'verify password'
)
)
);
// PASSWORD CONFIRMATION VALIDATION FUNCTION
public function matchPasswords($data){
if ($data['password'] == $this->data['User']['password_confirmation']) {
return True;
}
$this->invalidate('password_confirmation', 'Your password do not match');
return FALSE;
}
}