Ответ 1
Вы можете использовать $this->input->is_ajax_request()
из класса input:
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
Я нахожусь в PHP script, и я хочу проверить, является ли запрос Ajax-запросом. (в основном, так как НЕ разрешить прямой доступ script, кроме вызова Ajax, который является...)
Итак, я определяю IS_AJAX
где-то в главном файле index.php
:
define('IS_AJAX',
isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
И затем, проверяя его в верхней части моего script:
if (!IS_AJAX) exit('No direct script access allowed');
Поскольку я новичок в CodeIgniter, я не уверен...
Вы можете использовать $this->input->is_ajax_request()
из класса input:
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
если вы хотите настроить запросы из вашего приложения codeigniter, попробуйте следующее: Вы должны создать крючок с именем Ajax_only.php в папке application/hooks
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'
);