DataMapper 和 Codeigniter 中的加密密码自定义规则
我正在使用 Codeigniter 2.0.3 和 DataMapper ORM 1.6.0。 CI 中的集成 DataMapper 已成功实现,除了密码加密外,一切正常。
我有一个名为 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();
编辑1:
的完整代码users.php
模型此处。
用户已成功存储在数据库中,但原始密码未加密。
我做错了什么?
谢谢,问候 马里奥
I am using Codeigniter 2.0.3 with DataMapper ORM 1.6.0. Integration DataMapper in CI has been implemented successfully and everything works fine, except password encryption.
I have model called users.php
with class Users
. I also have set validation rules:
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')
)
);
My encrypt function in validation rule:
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
table structure:
id
email
password
last_login
created_on
I simple create new user via controller:
$u = new Users();
$u->email = '[email protected]';
$u->password = 'mario';
$u->save();
EDIT 1:
Full code of users.php
model here.
User successfully stored in the database but with original password not encrypted.
What am I doing wrong?
Thanks, Regards
Mario
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您的
salt
类变量丢失了。在您的_encrypt
函数中,您调用$this->salt
,但是,您从未在模型中声明 salt。我认为您需要在类中添加一个salt
变量:I believe it's that your
salt
class variable is missing. In your_encrypt
function, you call$this->salt
, however, you never declare salt in the model. I think you need to add asalt
variable in the class:我解决了将 DataMapper 升级到 1.8.x 版本的问题。现在,它工作得很好!
I solved problem with upgrading DataMapper to 1.8.x version. Now, it works just fine!