【问题标题】:Codeigniter showing error when I try to resubmit form with csrf_protection set to true当我尝试将 csrf_protection 设置为 true 时重新提交表单时,Codeigniter 显示错误
【发布时间】:2017-12-28 22:33:17
【问题描述】:

我的 CI 网站有 csrf 保护。

$config['csrf_protection'] = TRUE;

所以,当我通过刷新重新提交表单时,我收到以下错误。

您请求的操作不被允许

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

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

这是我的课程,位于 application/core/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']);         
        }        
    }
}

我收到以下错误

在非对象上调用成员函数 library()

【问题讨论】:

    标签: codeigniter csrf-protection


    【解决方案1】:

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

    文件位置: application\core\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);
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2020-04-08
        • 2014-01-06
        • 1970-01-01
        • 1970-01-01
        • 2013-04-18
        • 2022-01-12
        • 1970-01-01
        • 1970-01-01
        • 2021-10-25
        相关资源
        最近更新 更多