【问题标题】:How to insert new user data into database in codeigniter 4如何在codeigniter 4中将新用户数据插入数据库
【发布时间】:2020-06-01 02:10:35
【问题描述】:

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

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

模型

 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(); ?>

【问题讨论】:

  • 加密库加载了吗?
  • 不...怎么做
  • 和 CodeIgniter 中的所有服务一样,它可以通过 Config\Services 加载,$encrypter = \Config\Services::encrypter();正如文档所说。
  • 完成但收到此错误Encrypter needs a starter key.
  • 设置密钥和驱动程序对您的加密服务至关重要。

标签: php authentication post encryption codeigniter-4


【解决方案1】:

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

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

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

顺便说一句,codeigniter 文档说:

请勿使用此加密库或任何其他加密库来存储密码!密码必须经过哈希处理,您应该通过 PHP 的密码哈希扩展来实现。

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

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

 $hashedpass = password_hash($password, PASSWORD_ARGON2I);

例如,要在登录时验证您的密码,请使用 password_verify 函数,它获取用户密码和哈希值并返回布尔值。

 password_verify($usepassword, $hash));

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

【讨论】:

  • 就像代码在某处出错...显示红色指示器public $config = new Config\Encryption(); public $config-&gt;key = 'aBigsecret_ofAtleast32Characters'; public $config-&gt;driver = 'OpenSSL'; public $encrypter = \Config\Services::encrypter($config);
  • 红色指示灯仍然存在
  • 你使用的密码哈希函数是什么?
  • 'password' =&gt; password_hash($this-&gt;request-&gt;getVar('password'), PASSWORD_BCRYPT), 这对我有用。现在我想验证用户是否已经存在...我该怎么做?
  • 我编辑了答案,看看如何验证用户的密码
猜你喜欢
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-04
  • 1970-01-01
  • 2013-04-04
  • 2016-09-18
  • 1970-01-01
相关资源
最近更新 更多