编码点火器显示错误,当我尝试重新提交csrf_protection设置为 true 的表单时



我的CI网站有csrf保护。

$config['csrf_protection'] = TRUE;

因此,当我通过刷新重新提交表单时,出现以下错误。

您请求的操作是不允许的

我希望它返回到最后一页,而不是显示此消息。

因此,我尝试通过扩展CI_Security文件来覆盖csrf_show_error()方法。

这是我位于应用程序/核心/My_Security.php中的类

class MY_Security extends CI_Security {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('user_agent');
    }
    public function csrf_show_error()
    {
        // show_error('The action you have requested is not allowed.');  // default code
        // force page "refresh" - redirect back to itself 
        // a page refresh restores the CSRF cookie      
        if ($this->agent->is_referral())
        {
            redirect(site_url());
        } else {            
            redirect($_SERVER['HTTP_REFERER']);         
        }        
    }
}

我收到以下错误

调用非对象上的成员函数库()

为了更改核心类,我在应用程序的核心文件夹中扩展了MY_Securtiy类。 并重定向到过去的页面。

文件位置:应用程序\核心\MY_Security.php

class MY_Security extends CI_Security {
    public function __construct()
    {
        parent::__construct();      
    }
    public function csrf_show_error()
    {
        header('Location: ' . htmlspecialchars($_SERVER['REQUEST_URI']), TRUE, 200);
    }
}

感谢您的解决方案,但是通过将新请求的请求类型更改为 GET,无论原始请求中使用的类型如何(例如 POST),返回代码 302 似乎更好。下一次刷新不会提出任何问题。

最新更新