使用yii将密码存储为哈希

时间:2015-12-19 17:22:52

标签: php yii

如何将密码存储为哈希?

<?php

class StudentRegisterForm extends CActiveRecord {

public $email;
public $password;
public $studentName;
public $academicYear;
public $moduleName;
private $_identity;

/**
 * Declares the validation rules.
 * The rules state that username and password are required,
 * and password needs to be authenticated.
 */
public static function model($className = __CLASS__) {
    return parent::model($className);
}

public function tableName() {
    return '{{students}}';
}

public function rules() {
    return array(
        // username and password are required
        array('email, password,studentName,academicYear,moduleName', 'required'),
    );
}

/**
 * Declares attribute labels.
 */
public function attributeLabels() {
    return array(
        'id' => 'Id',
        'email' => 'Email',
        'password' => 'Password',
        'studentName' => 'Student Name',
        'academicYear' => 'Acadamic Year',
        'moduleName' => 'Module Name',
    );
}

    /**
 * Checks if the given password is correct.
 * @param string the password to be validated
 * @return boolean whether the password is valid
 */
public function validatePassword($password) {
    return CPasswordHelper::verifyPassword($password, $this->password);
}

/**
 * Generates the password hash.
 * @param string password
 * @return string hash
 */
public function hashPassword($password) {
    return CPasswordHelper::hashPassword($password);
}

//     * This is invoked before the record is saved.
//     * @return boolean whether the record should be saved.
public function beforeSave() 
{
    if (parent::beforeSave()) {

        return true;
    }
    else
        return false;
}

//     * This is invoked after the record is saved.

public function afterSave() {
    parent::afterSave();
}
}

0 个答案:

没有答案
相关问题