yii 中由 md5 加密两次的密码 [英] password encrypted twice by md5 in yii

查看:48
本文介绍了yii 中由 md5 加密两次的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用 Yii 框架开发一个应用程序.我创建了一个注册表单,其中有一个密码字段.注册后,我看到存储在db中的密码结果被md5加密了两次.

I have been developing an application in Yii framework. I create a registration form where there is a field that is password. After registration, I saw that password result stored in db is being encrypted twice of md5.

我在模型中写道:

protected function afterValidate()
{
    $this->password = $this->encrypt($this->password);
}
public function encrypt($value)
{
    return md5($value);
}

一个控制器

public function actionRegistration()
{
    $model=new User('registration');

    // Uncomment the following line if AJAX validation is needed
   $this->performAjaxValidation($model);

    $model->scenario = 'registerwcaptcha';
    if(isset($_POST['User']) )
    {
        $model->attributes=$_POST['User'];
        $keystring = md5( rand(0,1000) ); // Generate random 32 character hash and assign it to a local variable.
        $model->keystring = $keystring;
        //$model->password = md5( $model->password );
        if($model->validate())
        {
            // and here is the actual HACKY part
            $model->scenario = NULL;

            // save user registration
            if($model->save())                                       
                $this->redirect(array('emailverify'));
        }

    }

    $this->render('registration',array(
        'model'=>$model,
    ));
}

谁能帮帮我.

推荐答案

最新版本的 Yii 内置了密码哈希.

The latest version of Yii has password hashing build in.

要散列,您可以使用:

$hash = CPasswordHelper::hashPassword($password);

并验证:

if (CPasswordHelper::verifyPassword($password, $hash)){
    // password matches with hash
}
else{
    // password doesn't match with hash
}

有关更多详细信息,请查看此页面:

For more detailed information take a look at this page:

http://www.yiiframework.com/doc/api/1.1/CPasswordHelper/

这篇关于yii 中由 md5 加密两次的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆