防止在Codeigniter中显示默认的CSRF错误页面 [英] Prevent from displaying the default CSRF error page in Codeigniter

查看:219
本文介绍了防止在Codeigniter中显示默认的CSRF错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当CSRF令牌过期时,我不喜欢默认情况下CI的行为。例如,当用户长时间显示登录表单并且他们最终提交它时,会出现一个带有错误消息的丑陋的空白页。



我要求解决方法,有人告诉我扩展安全类,并覆盖其 csrf_show_error()方法,如下所示:

  class MY_Security extends CI_Security {


public function __construct()
{
parent :: __ construct ();

}

public function csrf_show_error()
{
//注释默认操作
// show_error已请求不允许。

//这里添加重定向动作
//我想在这里使用一些帮助函数redirect()

}

}

问题是我不能,或者我不知道如何获取到这里的库和助手,以便执行重定向。我不能在这里使用 get_instance()函数,因为我得到一个错误。所以,我还有什么couls我做?

解决方案

核心类如 CI_Security 在帮助程序和库之前实例化 - 无法像CI应用程序中其他位置那样使用这些函数。



使用本地PHP函数(如 header())重复类中的功能,如果你只是想重定向到一个更漂亮的错误页面,这不是一件困难。 / p>

I don't like how CI behaves by default when a CSRF token has expired. For instance, when a user has displayed the login form for a long time and they finally submit it, this ugly blank page with the error message comes up.

I asked for ways to get around this, and someone told me to extend the Security class, and override its csrf_show_error() method, like this:

class MY_Security extends CI_Security {


    public function __construct()
    {
        parent::__construct();

    }

    public function csrf_show_error()
    {
        // comment out the default action
        // show_error('The action you have requested is not allowed.');

        // add redirect actions here
        // I'd like to use some helper function here like redirect()

    }

} 

The problem is that I can't, or I don't know how to get access to the libraries and helpers from here, in order to perform a redirect. I can't use the get_instance() function here because I get an error. So, what else couls I do? Or is there any other better option to prevent from showing this error page?

解决方案

Core classes like CI_Security are instantiated before helpers and libraries - there is no ability to utilize these functions like you would elsewhere in a CI app.

You'll have to duplicate the functionality in the class using native PHP functions like header() which is not much of a hardship if you'd simply like to redirect to a prettier error page.

这篇关于防止在Codeigniter中显示默认的CSRF错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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