Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在cakephp 2.x版本中加密密码_Cakephp_Cakephp 2.0 - Fatal编程技术网

如何在cakephp 2.x版本中加密密码

如何在cakephp 2.x版本中加密密码,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,大家好,我正在使用cakephp 2.x,因为我是新来这里的,我需要在密码存储到数据库之前对其进行加密 User.ctp:我是这样发布的 <?php echo $this->Form->input('password',array('type'=>'password','label'=>false,'div'=>false,'class'=>'form-control','id'=>'password')); ?> 如何加密密码文件,以

大家好,我正在使用cakephp 2.x,因为我是新来这里的,我需要在密码存储到数据库之前对其进行加密

User.ctp:我是这样发布的

<?php
  echo $this->Form->input('password',array('type'=>'password','label'=>false,'div'=>false,'class'=>'form-control','id'=>'password'));
?>

如何加密密码文件,以及如何在您使用
CakePhp时使用该模型,以符合框架的最佳实践

创建新用户记录时,可以在 在使用适当的密码哈希器保存模型的回调之前 类别:

在调用
$this->Auth->login()
之前,您不需要散列密码。各种身份验证对象将分别散列密码

如果您使用的身份验证模型与
User
不同,则需要在AppController中定义该模型。在您的情况下,需要在AppController中执行以下操作:

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");
 }
}
$this->Auth->authenticate = array(
'Form' => array('userModel' => 'Login')
);
如果要散列密码,请尝试以下操作:

$hashedPassword = AuthComponent::password('original_password');

请看这里:。

Manohar Khadka的答案()很好。但是我建议使用hash这个词而不是encrypt。密码永远不应该以可以再次检索的形式保存Yeah Manohar khadka的答案很好,但是当我使用哈希时,密码只与哈希的密码一起工作,而与真实的密码不一起工作。(例如:-Passowd:hello123哈希::9ee143147311a6ddd3B74b830674c887ba972b49dab4d826d0b7e334a789e)如果我输入hello123它就不会登录如果输入hash only它就登录@NicholasIt听起来你是在把密码和hash进行比较,这(大概)永远不会起作用。您需要将登录尝试中的密码哈希值与用户存储的密码哈希值进行比较。有一个错误,密码正在哈希化,但当我尝试登录时,它显示错误,只有在我们输入哈希化的密码时,它才登录。并请指定解密方法。是否仍有错误?数据库中是否有“密码”字段?如果使用其他字段,请参见此处。。是的,在数据库中,我有密码字段,正如我所说,我可以使用散列的密码和原始密码登录,如果输入密码,它将显示错误的密码,如果我输入散列的密码,它将登录。AppController中的组件数组如何?你把上面的代码放在哪里了?我已经很清楚你在说什么了..你不需要在验证之前把它散列。。试试最新的答案。。Cakephp大会就在这里。
$hashedPassword = AuthComponent::password('original_password');