簡體   English   中英

代碼點火器403覆蓋狀態

[英]codeigniter 403 override status

我知道這個問題以前曾被問過。 但是,對於這個問題,完全沒有任何好的答案。

我正在使用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嗎?

謝謝

所有內置錯誤頁面均使用模板,這些模板位於application / views / errors /下 -您可以編輯模板以更改外觀。

CSRF錯誤頁面特別使用“ error_general”模板。

雖然不能將控制器用於404頁面。

這是我的處理方式。 我使用以下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);
  }

}

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM