如何在cakephp 2.x版本中加密密码 [英] How to encrypt a password in cakephp 2.x version
问题描述
大家好,我正在使用cakephp 2.x,因为我是新来的用户,在将密码存储到数据库之前,我需要先对其密码进行加密
Hello everyone i am using cakephp 2.x, as i am new to here, i need to encrypt my password before it stores to database
User.ctp:我要像这样发布帖子
User.ctp : I am posting like this to post
<?php
echo $this->Form->input('password',array('type'=>'password','label'=>false,'div'=>false,'class'=>'form-control','id'=>'password'));
?>
控制器:
public function setting()
{
$this->layout='setting_template';
if($this->Session->read('username')==""){
$this->redirect(array('action' => 'user_login'));
}
elseif ($this->Session->read('username') == "admin" )
{
if($this->request->is('post'))
{
$this->data['password'] = encrypt($this->data ['password']);
if ($this->Login->save($this->request->data)) {
$this->Session->setFlash('The user has been saved');
$this->redirect(array('action' => 'setting'));
} else {
$this->Session->setFlash('The user could not be saved. Please, try again.');
}
}
$opp=$this->Login->find('all');
$this->set('login',$opp);
}
else{
echo "<script type='text/javascript'> alert('Permission Denied'); </script>";
$this->redirect(array('action' => 'index'));
}
}
登录控制器:
public function login()
{
$this->layout='login_template';
if($this->data)
{
$this->Session->write('id',$this->data['Login']['id'] );
$results = $this->Login->find('first',array('conditions' => array('Login.password' => $this->data['Login']['password'],'Login.username' => $this->data['Login']['username'])));
$this->Session->write('name',$results['Login']['name']);
if ($results['Login']['id'])
{
$this->Session->write($this->data['Login']['username'].','. $this->data['Login']['password']);
$this->Session->write('username',$this->data['Login']['username']);
$this->redirect(array('action'=>'index'));
}
else
{
$this->Session->setFlash("error");
}
}
我如何加密密码文件以及如何加密使用模型
How can i encrypt the password file and also how can use the Model
推荐答案
在使用 CakePhp
时,请选择框架最好的
As you are using CakePhp
go with framework's best practices.
创建新的用户记录时,您可以使用适当的密码哈希器$散列
中的密码,然后保存模型的回调b $ b类:
When creating new user records you can hash a password in the beforeSave callback of your model using appropriate password hasher class:
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
public function beforeSave($options = array()) {
if (!empty($this->data[$this->alias]['password'])) {
$passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}
}
在调用 $ this-> Auth-> login()
。
如果您使用与 User
不同的模型进行身份验证,则需要在AppController中定义。在您的案例中,您需要在AppController中执行以下操作:
If you are using different model than User
for authentication you need to define that in AppController. In your Case you need to do something like this in AppController:
$this->Auth->authenticate = array(
'Form' => array('userModel' => 'Login')
);
如果您希望对密码进行哈希处理,请尝试以下操作:
If you wish to hash your password, try this:
$hashedPassword = AuthComponent::password('original_password');
在这里查看: Cakephp密码哈希。
这篇关于如何在cakephp 2.x版本中加密密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!