询问CakePHP-使用密码字段
我在这里搜索了很多话题,但我不能解决我的问题。请帮我查一下这个 我做了注册页面,当我做密码字段时 我有用户\u controller.php像:询问CakePHP-使用密码字段,php,mysql,cakephp,Php,Mysql,Cakephp,我在这里搜索了很多话题,但我不能解决我的问题。请帮我查一下这个 我做了注册页面,当我做密码字段时 我有用户\u controller.php像: class UsersController extends AppController { var $name = "Users"; var $helpers = array('Paginator','Html'); var $paginate = array(); //Doi tuong component de
class UsersController extends AppController
{
var $name = "Users";
var $helpers = array('Paginator','Html');
var $paginate = array();
//Doi tuong component de thuc thi thao tac login
public $components = array
(
'Auth' => array
(
'authorize' => 'controller',
'loginRedirect' => array
(
'admin' => FALSE,
'controller' => 'users',
'action' => 'dashboard'
),
'loginError' => 'Invalid account',
'authError' => 'You don\'t have permission'
),
'Session'
);
//Ham loc cac user truoc khi truy cap trang
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('add');
$this->Auth->allow('viewuserall');
}
//Ham them moi user
public function add()
{
$this->layout = 'TDCake';
$this->User->set($this->data);
if($this->User->valid_user() == TRUE)
{
if(!empty($this->data))
{
$this->User->create();
if($this->User->save($this->data))
{
$this->Session->setFlash('User has been created!');
$this->redirect(array('action'=>'login'));
}
else
{
$this->Session->setFlash('Please correct the errors');
}
};
}
else
{
$this->Session->setFlash("Your data is NOT available");
}
}
//Ham login cho user
public function login()
{
$this->layout = 'TDCake';
if
(
!empty($this->data) &&
!empty($this->Auth->data['User']['username'])&&
!empty($this->Auth->data['User']['password'])
)
{
$user = $this->User->find
(
'first',array
(
'conditions'=>array
(
'User.email'=>$this->Auth->data['User']['username'],
'User.password'=>$this->Auth->data['User']['password']
),
'recursive' => -1
)
);
if(!empty($user) && $this->Auth->login($user))
{
if($this->Auth->autoRedirect)
{
$this->redirect($this->Auth->redirect());
}
}
else
{
$this->Session->setFlash
(
$this->Auth->loginError,
$this->Auth->flashElement,
array(),'auth'
);
}
}
}
//Ham logout cho user
public function logout()
{
$this->redirect($this->Auth->logout());
}
//Ham gi cha biet, de do tinh sau =))
public function dashboard()
{
$this->layout = 'TDCake';
}
//Ham view cac user khong dieu kien trong table users
function viewuserall()
{
$this->layout = 'TDCake';
$this->paginate=array
(
'limit' => 10,
'order' => array('id' => 'asc'),
);
$data = $this->paginate("User");
$this->set("data",$data);
}
}
模型中的User.php是:
class User extends AppModel
{
var $name = "User";
var $validate = array();
function validate_passwords()
{
if($this->data[$this->alias]['pass'] == $this->data[$this->alias]['rpass'])
{
return $this->data[$this->alias]['pass'] = $this->data['User']['password'];
}
else return FALSE;
}
function valid_user()
{
$this->validate = array
(
//Kiem tra username truoc khi add
'username' => array
(
'rule01_notEmpty' => array
(
'rule' => 'notEmpty',
'message' => 'You must enter your Username !'
),
'rule02_max16' => array
(
'rule' => array('maxLength', 20),
'message' => 'Your Username must be less than 20 chars !'
),
'rule03_exists' => array
(
'rule' => 'isUnique',
'message' => 'Your Username have already existed !'
)
),
//Kiem tra email truoc khi add
'email' => array
(
'rule01_notEmpty' => array
(
'rule' => 'notEmpty',
'message' => 'You must enter your Email !'
),
'rule02_exists' => array
(
'rule' => 'isUnique',
'message' => 'Your Email have already existed !'
),
'rule03_emailtype' => array
(
'rule' => 'email',
'message' => 'You didn\'t type a email !'
)
),
//Kiem tra password truoc khi add
'pass' => array
(
'length' => array
(
'rule' => array('between', 6, 20),
'message' => 'Your password must be between 8 and 40 characters.',
),
),
'rpass' => array
(
'length' => array
(
'rule' => array('between', 6, 20),
'message' => 'Your password must be between 8 and 40 characters.',
),
'compare' => array
(
'rule' => 'validate_passwords',
'message' => 'The passwords you entered do not match.',
)
)
);//End this->validate=array
if($this->validates($this->validate==TRUE))
{
return TRUE;
}
else
{
return FALSE;
}
}//End function valid_user
}
添加.ctp是
echo $this->Session->flash('auth');
echo $this->Form->create();
echo $this->Form->input('username', array('label' => ('Username')));
echo $this->Form->input('email', array('label' => ('Email')));
echo $this->Form->input('pass', array('label' => ('Password'),'type' => 'password', 'value' => ''));
echo $this->Form->input('rpass', array('label' => ('Repeat Password'), 'type' => 'password', 'value' => ''));
echo $this->Form->input('firstname', array('label' =>('Firstname')));
echo $this->Form->input('lastname', array('label' =>('Lastname')));
echo $this->Form->input('dob', array('label' =>('DOB'),'type' => 'date'));
echo $this->Form->end('Register');
说明:
因此,在本例中,我可以验证2个密码字段(空、不相等,…),但它不能插入数据库。这意味着它将当前数据插入数据库,但数据库中的密码列为空。在数据库中,我的密码列名“password”也是
在另一种情况下,我将密码的名称“pass”改为“password”
当然,我已经改变了任何与…有关的地方
在这种情况下,它可以插入密码,但不能验证任何内容
我对此太困惑了……我不知道我错在哪里……谁能帮我一下。我不知道你为什么要在验证函数中执行赋值:
return $this->data[$this->alias]['pass'] = $this->data['User']['password'];
即使你在做作业,也应该是:
return $this->data['User']['password'] = $this->data[$this->alias]['pass'];
认识到字段“password”是从包含信息的$this->data
获取值,而不是相反
还有。最好(就清晰性而言)将此代码分成两行
$this->data['User']['password'] = $this->data[$this->alias]['pass'];
return $this->data['User']['password'];
如果字段在数据库中被称为密码,则应将其准确命名为“password”;如果未明确指定,则应将其命名为“password”
您的add函数没有执行上述操作,而且,作为最佳实践,您应该对密码进行哈希运算
请参阅CakePHP教程和示例手册
花点时间仔细阅读所有的片段和建议。别忘了标准。:) 也许吧。如果你是初学者并且害怕出错,这也会很有用。对不起,我不明白你的意思。谢谢,我已经解决了这个问题。现在它只是一个小的。我的“rpass”是告诉错误。当插入到DB时,我如何忽略这个字段。您可以使用“unSET($DATA [Field]));“在保存之前删除字段。我已经尝试过了,但它将是空白参数,并且它会将空数据插入到DB中。但在我的数据库中没有类似于此名称的列。那是因为它犯了那个错误。我只想问一下,在准备保存时,如何忽略参数rpass。请我想我不明白你的意思。您希望忽略“rpass”,但希望将该值插入数据库??我猜您的意图是保存rpass->password,因此,分配值:$user['user']['password']=$data['user']['rpass'];,然后保存
$this->data['User']['password'] = $this->data[$this->alias]['pass'];
return $this->data['User']['password'];