DataMapper 和 Codeigniter 中的加密密码自定义规则

发布于 2024-12-09 11:59:12 字数 1536 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2024-12-16 11:59:12

我相信您的 salt 类变量丢失了。在您的 _encrypt 函数中,您调用 $this->salt,但是,您从未在模型中声明 salt。我认为您需要在类中添加一个 salt 变量:

// in your Users model
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');

class Users extends DataMapper {

    var $table = 'users';
    var $has_one = array('profile');
    var $salt = 'B1471tU77IK1411'; // any string - helps to make password encryption stronger

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 a salt variable in the class:

// in your Users model
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');

class Users extends DataMapper {

    var $table = 'users';
    var $has_one = array('profile');
    var $salt = 'B1471tU77IK1411'; // any string - helps to make password encryption stronger
掀纱窥君容 2024-12-16 11:59:12

我解决了将 DataMapper 升级到 1.8.x 版本的问题。现在,它工作得很好!

I solved problem with upgrading DataMapper to 1.8.x version. Now, it works just fine!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文