CodeIgniter 開啟 $autoload['libraries'] = ['session'] 後報錯:「In order to use the Session class you are required to set an encryption key」。原因是 Session 類依賴加密庫,必須配置 encryption_key。
編輯 application/config/config.php,設置加密密鑰:
$config['encryption_key'] = 'your_strong_random_key_here';
openssl_random_pseudo_bytes(32) 或線上工具/* |-------------------------------------------------------------------------- | Encryption Key |-------------------------------------------------------------------------- | | If you use the Encryption class or the Session class you | MUST set an encryption key. See the user guide for info. | */ $config['encryption_key'] = 'xxxxx'; // 在這裡設置
$config['sess_driver'] = 'files'; // 或 database/redis/memcached $config['sess_cookie_name'] = 'ci_session'; $config['sess_expiration'] = 7200; // 2小時 $config['sess_save_path'] = APPPATH . 'cache/sessions/'; // files 驅動路徑 $config['sess_match_ip'] = FALSE; $config['sess_time_to_update'] = 300; $config['sess_regenerate_destroy'] = FALSE;
| 現象 | 原因 | 解決 |
|---|---|---|
| Session 不生效 | sess_save_path 目錄不可寫 | chmod 777 或修改驅動 |
| 頻繁掉線 | sess_expiration 過短 | 調大或啟用 sess_time_to_update |
| 多服務器不同步 | files 驅動不共享 | 改用 database/redis 驅動 |