簡體   English   中英

檢查CodeIgniter中的Ajax請求

[英]Check for Ajax request in CodeIgniter

我在PHP腳本中,我想檢查請求是否是Ajax請求。 (基本上不允許直接腳本訪問,而不是Ajax調用。)

所以,我在主index.php文件中的某個地方定義了IS_AJAX

define('IS_AJAX', 
       isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
       strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

然后在我的腳本頂部檢查它:

if (!IS_AJAX) exit('No direct script access allowed');

由於我是CodeIgniter的新手,我想知道:

  • 有沒有這樣的內置功能?
  • 有更優雅的方式嗎?

您可以使用輸入類中的$this->input->is_ajax_request()

if (!$this->input->is_ajax_request()) {
   exit('No direct script access allowed');
}

如果使用鈎子(CI docs),if (!$this->input->is_ajax_request())為每個AJAX方法添加if (!$this->input->is_ajax_request()) 這是基於Jorge的解決方案 ,稍作改進:

config/config.php

通過更改默認值(從FALSE )啟用CI掛鈎:

$config['enable_hooks'] = TRUE;

config/hooks.php

最后添加以下內容:

$hook['post_controller_constructor'] = array(
    'class' => 'Ajax_only',
    'function' => 'show_404_on_illegal_ajax',
    'filename' => 'Ajax_only.php',
    'filepath' => 'hooks'
);

post_controller_constructor :在實例化控制器之后立即調用 ,但在任何方法調用發生之前調用

config/ajax_methods.php

創建一個新的配置文件,其中包含只應在發出AJAX請求時調用的所有控制器和方法:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
|--------------------------------------------------------------------------
| References to all AJAX controllers' methods or the controller itself
|--------------------------------------------------------------------------
|
| Based on Jorge's solution: https://stackoverflow.com/a/43484330/6225838
| Key: controller name
| Possible values:
| - array: method name as key and boolean as value (TRUE => IS_AJAX)
| - boolean: TRUE if all the controller's methods are for AJAX requests
|
*/
$config['welcome'] = [
  'index' => FALSE, // or 0 -> this line can be removed (just for reference)
  'ajax_request_method_1' => TRUE, // or 1
  'ajax_request_method_2' => TRUE, // or 1
];
$config['ajax_troller'] = TRUE;

hooks/Ajax_only.php

創建鈎子本身,它檢測當前控制器和/或其方法是否存在於上面的新配置文件中。 如果是這樣,當當前請求不是AJAX並且方法/控制器在配置中具有值時,它顯示404默認頁面:

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

class Ajax_only {

  public function __construct()
  {
    $this->CI = &get_instance();
    $this->CI->config->load('ajax_methods');
  }

  public function show_404_on_illegal_ajax()
  {
    $fetched_troller_val = $this->CI->config->item(
      $this->CI->router->fetch_class()
    );
    $fetched_method = $this->CI->router->fetch_method();

    $is_ajax_method = is_array($fetched_troller_val) &&
        // verify if the method's name is present
        isset($fetched_troller_val[$fetched_method]) &&
        // verify if the value is truthy
        $fetched_troller_val[$fetched_method];

    // if the controller is not in the config file then
    // config->item() returned NULL
    if($fetched_troller_val !== NULL &&
        $this->CI->input->is_ajax_request() === FALSE  &&
        ($fetched_troller_val === TRUE || $is_ajax_method)
      ) {
      show_404();
    }
  }
}

如果要自定義來自codeigniter應用程序的請求,請嘗試以下操作:您必須在application / hooks文件夾中創建名為Ajax_only.php的掛鈎

class Ajax_only {
    private $_controllers = [];

    private $CI;

    public function __construct() {
        $this->CI =& get_instance();
    }

    public function eval_request() {
        $controller = $this->CI->router->fetch_class();
        $method = $this->CI->router->fetch_method();
        if ( array_key_exists( $controller, $this->_controllers ) && $this->CI->input->is_ajax_request() === FALSE  ) {
            if ( ( $this->_controllers[ $controller ] === TRUE || ( is_array( $this->_controllers[ $controller ] ) && array_key_exists( $method, $this->_controllers[ $controller ] ) && $this->_controllers[ $controller ][ $method ] === TRUE ) ) ) {
                show_404();
            }
        }
    }
}


/*Examples
 * $_controllers = [
 *      'my_controller_name' => TRUE //all methods must be ajax
 *      'my_controller_name  => [
 *          'method_name' => TRUE //only the selected methods must be ajax
 *      ]
 * ]
 */

並配置您的application / config / hooks.php文件

$hook['post_controller_constructor'] = array(
    'class' => 'Ajax_only',
    'function' => 'eval_request',
    'filename' => 'Ajax_only.php',
    'filepath' => 'hooks'
);

暫無
暫無

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

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