【问题标题】:how to make confirm password validation cakephp with hashing it如何通过散列确认密码验证 cakephp
【发布时间】:2016-12-19 03:54:53
【问题描述】:

我正在使用 cakephp 2.xx,我想在进入数据库之前使用 sha256 对密码进行哈希处理, 在此之前我想在我的表单输入中设置验证值密码,验证检查密码输入并重新确认密码是否匹配,如果在我的控制器中,当表单捕获验证时,密码会自动散列

if ($this->request->data['Driver']['password'] != $this->request->data['Driver']['confirm_password']) {
      $this->request->data['Driver']['password'] = hash('sha256',$this->request->data['Driver']['password']);
}

当表单没有捕获时,密码哈希是必要的,那么我如何在我的模型中进行验证?

提前致谢。

【问题讨论】:

    标签: php validation cakephp hash sha256


    【解决方案1】:

    在您的模型中 (Driver.php)

    验证

    <?php 
        public $validate = array(
    
            'password' => array(
                'notempty' => array(
                    'rule' => array('notempty'),                            
                ),
                'password_confirm'=>array(
                    'rule'=>array('password_confirm'),
                    'message'=>'Password Confirmation must match Password',                         
                ),    
            ),      
        );
    ?>
    

    自定义验证规则

    <?php 
        public function password_confirm(){ 
            if ($this->data['Driver']['password'] !== $this->data['Driver']['password_confirmation']){
                return false;       
            }
            return true;
        }
    ?>
    

    散列,但我认为最好选择 AuthComponent

    <?php 
        public function beforeSave($options = array()) {        
            $this->data['Driver']['password'] = hash('sha256',$this->data['Driver']['password']);   
            return true;        
        }
    ?>
    

    这是总体描述,您可能需要修改其中的某些部分

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 2020-02-29
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 2013-09-18
      • 2015-04-03
      相关资源
      最近更新 更多