繁体   English   中英

如何在 codeigniter 中将新用户数据插入数据库 4

[英]How to insert new user data into database in codeigniter 4

我正在尝试将用户数据发布到数据库中,但密码部分出现错误,请帮助

Call to undefined method App\Controllers\Register::encrypt() 

model

 public function register()
    {
        $model = new RegisterModel();

        if (!$this->validate([
            'username' => 'required|min_length[3]|max_length[25]',
            'password'  => 'required',
            'user_phone' => 'required|min_length[11]|max_length[11]'

        ])) {
            echo view('templates/header', ['page_title' => 'Register']);
            echo view('dashboard/register');
            echo view('templates/footer');
        } else {
            $model->save([
                'username' => $this->request->getVar('username'),
                'password'  => $this->encryption->encrypt($this->input->post('password')),
                'user_phone'  => $this->request->getVar('user_phone'),
            ]);

            echo view('news/success');
        }
    }

如果用户已经存在,这不会报告任何内容

<?= \Config\Services::validation()->listErrors(); ?>

Go 到 app/config/encryption.php 并设置您的密钥和驱动程序。

或者

您可以通过将您自己的配置 object 传递给 Services 调用来替换配置文件的设置。 $config 变量必须是 Config\Encryption class 或扩展 CodeIgniter\Config\BaseConfig 的 object 的实例。

$config         = new Config\Encryption();
$config->key    = 'aBigsecret_ofAtleast32Characters';
$config->driver = 'OpenSSL';
$encrypter = \Config\Services::encrypter($config);

顺便说一句 codeigniter 文档说:

不要使用此加密库或任何其他加密库来存储密码,密码必须经过哈希处理。 你应该通过 PHP 的密码散列扩展来做到这一点。

password_hash() 使用强大的单向散列算法创建一个新密码 hash。 password_hash() 与 crypt() 兼容。 因此,由 crypt() 创建的密码哈希可以与 password_hash() 一起使用。

password_hash 支持各种 hash 算法,你可以使用其中的任何一种,这里是一个例子。

 $hashedpass = password_hash($password, PASSWORD_ARGON2I);

例如,要在登录时验证您的密码,请使用 password_verify function 它获取用户密码和 hash 并返回 boolean 值。

 password_verify($usepassword, $hash));

有关散列密码的更多详细信息,请参阅此链接: https://www.php.net/manual/en/function.password-hash.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM