升级加密
文档
变更点
- 不再支持 - MCrypt,它在 PHP 7.2 中已被弃用。
升级指南
- 在配置中, - $config['encryption_key'] = 'abc123';从 application/config/config.php 移到了 app/Config/Encryption.php 中的- public $key = 'abc123';。
- 如果需要解密用 CI3 加密的数据,请配置设置以保持兼容性。参见 用于与 CI3 保持兼容性的配置。 
- 在使用加密库的任何地方,都必须将 - $this->load->library('encryption');替换为- $encrypter = service('encrypter');,并如下例代码中更改加密和解密的方法。
代码示例
CodeIgniter 3.x 版本
<?php
$this->load->library('encryption');
$plain_text = 'This is a plain-text message!';
$ciphertext = $this->encryption->encrypt($plain_text);
// Outputs: This is a plain-text message!
echo $this->encryption->decrypt($ciphertext);
CodeIgniter 4.x 版本
<?php
$encrypter = service('encrypter');
$plainText  = 'This is a plain-text message!';
$ciphertext = $encrypter->encrypt($plainText);
// Outputs: This is a plain-text message!
echo $encrypter->decrypt($ciphertext);