检查CodeIgniter中的Ajax请求

Dr.*_*eon 30 php ajax codeigniter

我在PHP脚本中,我想检查请求是否是Ajax请求.(基本上不允许直接脚本访问,而不是Ajax调用.)

所以,我IS_AJAX在主index.php文件中的某个地方定义:

define('IS_AJAX', 
       isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
       strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
Run Code Online (Sandbox Code Playgroud)

然后在我的脚本顶部检查它:

if (!IS_AJAX) exit('No direct script access allowed');
Run Code Online (Sandbox Code Playgroud)

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

  • 有没有这样的内置功能?
  • 有更优雅的方式吗?

Yan*_*erk 120

您可以$this->input->is_ajax_request()输入类中使用:

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

  • is_ajax_request()不是安全措施.有一个类似的答案,其中返回404,这将避免给人一种误导性的印象.(HTTP/1.1"当服务器不希望明确拒绝请求的原因,或者没有其他响应适用时,通常会使用此状态代码")http://stackoverflow.com/questions/6555652/controller-方法 - 即,在只称为逐Ajax的化妆私人/ 8072539#8072539 (3认同)

CPH*_*hon 6

如果您使用hooks (CI docs),无需if (!$this->input->is_ajax_request())为每个 AJAX 方法添加一个。这是基于Jorge的解决方案在这里有一些细微的改进:

config/config.php

通过更改默认值(从FALSE)启用 CI 挂钩:

$config['enable_hooks'] = TRUE;
Run Code Online (Sandbox Code Playgroud)

config/hooks.php

在最后添加以下内容:

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

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: /sf/answers/3043903131/
| 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;
Run Code Online (Sandbox Code Playgroud)

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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)