我在DataMapper ORM 1.6.0中使用Codeigniter 2.0.3。在CI中集成DataMapper已成功實現,除密碼加密外,一切正常。在DataMapper和Codeigniter中加密密碼自定義規則
我有模型叫users.php
與類Users
。我還設置驗證規則:
var $validation = array(
array(
'field' => 'password',
'label' => 'Password',
'rules' => array('required', 'trim', 'unique', 'min_length' => 5, 'encrypt'),
),
array(
'field' => 'email',
'label' => 'Email Address',
'rules' => array('required', 'trim', 'valid_email')
)
);
我的加密功能的驗證規則:
function _encrypt($field)
{
// Don't encrypt an empty string
if (!empty($this->{$field}))
{
// Generate a random salt if empty
if (empty($this->salt))
{
$this->salt = md5(uniqid(rand(), true));
}
$this->{$field} = sha1($this->salt . $this->{$field});
}
}
MySQL的users
表結構:
id
email
password
last_login
created_on
我簡單的通過控制器創建新的用戶:
$u = new Users();
$u->email = '[email protected]';
$u->password = 'mario';
$u->save();
EDIT 1:
完整代碼的users.php
模型here。
用戶成功存儲在數據庫中,但原始密碼未加密。
我在做什麼錯?
感謝,問候 馬里奧
我們可以看到完整的'users.php'模型嗎?你的代碼是正確的,所以我假設無法找到驗證規則。 – swatkins
@swatkins我已經用完整'users.php'模型代碼鏈接更新了我的第一篇文章。 – Puzo