簡體   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