CodeIgniter3启用CSRF保护触发CI_Controller未找到致命错误
CodeIgniter 3中登录/注册表单启用CSRF保护时的CI_Controller未找到问题
问题背景
我基于CodeIgniter 3开发系统,需要为登录和注册表单启用CSRF保护,于是在配置文件中做了如下设置:
$config['csrf_protection'] = FALSE; if (isset($_SERVER["REQUEST_URI"])) { if (stripos($_SERVER["REQUEST_URI"], '/login') !== FALSE || stripos($_SERVER["REQUEST_URI"], '/register') !== FALSE ) { $config['csrf_protection'] = TRUE; } }
但一旦将csrf_protection配置项设为true,错误处理器就触发函数不存在的提示——该函数实际存在于MY_url_helper中,真正的问题是CI_Controller未被找到,尽管回溯信息显示CodeIgniter已加载:
Array ( [0] => Array ( [file] => path\to\system\core\Exceptions.php [line] => 268 [function] => include ) [1] => Array ( [file] => path\to\system\core\Common.php [line] => 627 [function] => show_php_error [class] => CI_Exceptions [object] => CI_Exceptions Object ( [ob_level] => 1 [levels] => Array ( [1] => Error [2] => Warning [4] => Parsing Error [8] => Notice [16] => Core Error [32] => Core Warning [64] => Compile Error [128] => Compile Warning [256] => User Error [512] => User Warning [1024] => User Notice [2048] => Runtime Notice ) ) [type] => -> [args] => Array ( [0] => Error [1] => Uncaught Error: Class "CI_Controller" not found in path\to\system\core\CodeIgniter.php:369 Stack trace: #0 path\to\app\core\MY_Exceptions.php(54): get_instance() #1 path\to\system\core\Common.php(195): MY_Exceptions->__construct() #2 path\to\system\core\Common.php(656): load_class('Exceptions', 'core') #3 [internal function]: _exception_handler(Object(Error)) #4 {main} thrown [2] => core/CodeIgniter.php [3] => 369 ) ) [2] => Array ( [file] => path\to\system\core\Common.php [line] => 693 [function] => _error_handler [args] => Array ( [0] => 1 [1] => Uncaught Error: Class "CI_Controller" not found in path\to\system\core\CodeIgniter.php:369 Stack trace: #0 path\to\app\core\MY_Exceptions.php(54): get_instance() #1 path\to\system\core\Common.php(195): MY_Exceptions->__construct() #2 path\to\system\core\Common.php(656): load_class('Exceptions', 'core') #3 [internal function]: _exception_handler(Object(Error)) #4 {main} thrown [2] => path\to\system\core\CodeIgniter.php [3] => 369 ) ) [3] => Array ( [function] => _shutdown_handler [args] => Array ( ) ) )
代码库扩展了CI_Exceptions类,测试用构造函数如下:
class MY_Exceptions extends CI_Exceptions { public function __construct() { parent::__construct(); die('Error is showing'); $this->CI =& get_instance(); die('Error not showing'); } }
可以看到,执行到$this->CI =& get_instance();时触发异常。我已查看相关问题,但无法采用现有解决方案。将CI错误日志级别设为4后,日志未显示任何错误:
INFO - 2023-10-06 13:47:57 --> Config Class Initialized INFO - 2023-10-06 13:47:57 --> Hooks Class Initialized DEBUG - 2023-10-06 13:47:57 --> UTF-8 Support Enabled INFO - 2023-10-06 13:47:57 --> Utf8 Class Initialized INFO - 2023-10-06 13:47:57 --> URI Class Initialized INFO - 2023-10-06 13:47:57 --> Router Class Initialized INFO - 2023-10-06 13:47:57 --> Output Class Initialized INFO - 2023-10-06 13:47:57 --> Security Class Initialized DEBUG - 2023-10-06 13:47:57 --> Global POST, GET and COOKIE data sanitized
求助解决思路
内容的提问来源于stack exchange,提问作者Kobus Myburgh
相关产品推荐
相关产品推荐

