当我尝试重新提交csrf_protection设置为true的表单时,Codeigniter显示错误

问题描述 投票:0回答:2

我的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']);         
        }        
    }
}

我收到以下错误

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

codeigniter csrf-protection
2个回答
2
投票

我没有更改核心类,而是在应用程序的核心文件夹中扩展了MY Security类。并重定向到过去的页面。

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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.