codeigniter 403覆盖状态

时间:2017-04-28 07:24:22

标签: php codeigniter

我知道之前已经问过这个问题。但完全没有任何好的答案可以解决这个问题。

我正在使用CSRF security并完全知道如何使用它。如果令牌无效,csrf将返回我

  

遇到错误

     

不允许您请求的操作。

并且在Security.php中有一个函数如何设置状态

public function csrf_show_error()
{
    show_error('The action you have requested is not allowed.', 403);
}

亲切的,我需要自定义错误页面。

如果我们设置config,就像$route['404_override'] = 'Page_error';一样 它会显示我们之前设置的Page_error

是否有任何情况如何覆盖403

谢谢

2 个答案:

答案 0 :(得分:0)

所有内置错误页面都使用位于 application / views / errors / 下的模板 - 您可以编辑这些模板以更改外观。

CSRF错误页面特别使用' error_general'模板。

除了404页面之外,不能使用控制器。

答案 1 :(得分:0)

这是我的处理方式。我使用以下PHP代码在应用程序/核心中创建了MY_Security.php文件:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Security extends CI_Security{

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

  public function csrf_show_error(){
    if((!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'))
      exit(json_encode(array(//put your array elements here)));
    else
      show_error('The action you have requested is not allowed.', 403);
  }

}

?>
相关问题