password encrypted twice by md5 in yii

2019-06-14 15:39发布

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.

I wrote in model as:

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

an in controller

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,
    ));
}

Could anybody help me please.

标签: php yii
1条回答
小情绪 Triste *
2楼-- · 2019-06-14 15:54

The latest version of Yii has password hashing build in.

To hash you can use:

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

and to verify:

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/

查看更多
登录 后发表回答