当我尝试将csrf_protection设置为true时重新提交表单时,Codeigniter显示错误 [英] Codeigniter showing error when I try to resubmit form with csrf_protection set to true

查看:79
本文介绍了当我尝试将csrf_protection设置为true时重新提交表单时,Codeigniter显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的CI网站具有csrf保护。

My CI website has csrf protection.

$config['csrf_protection'] = TRUE;

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

So, when I resubmit form by refresh I am getting the following error.


不允许您请求的操作

The action you have requested is not allowed

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

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

So, I try to override csrf_show_error() method by extending the CI_Security file.

这是我的类,位于 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()

Call to a member function library() on a non-object


推荐答案

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

Insted of changing the core classes, I extended the MY_Securtiy class in core folder of application. and redirecting to past page.

文件位置: application\core\MY_Security.php

File Location: 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);
    }
}

这篇关于当我尝试将csrf_protection设置为true时重新提交表单时,Codeigniter显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆