php 如何检查请求是否是通过 CodeIgniter 中的 AJAX 发出的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4301150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:34:57  来源:igfitidea点击:

How do I check if the request is made via AJAX in CodeIgniter?

phpajaxcodeigniter

提问by jameserie

How do I check if the request is an AJAX? I am using CodeIgniter. I have a link that when it clicked, it'll open the pop-up dialog window this is done through ajax it requests to a controller name login_window().

如何检查请求是否为 AJAX?我正在使用 CodeIgniter。我有一个链接,当它点击时,它会打开弹出对话框窗口,这是通过 ajax 完成的,它请求控制器名称login_window()

CodeIgniter

代码点火器

//Here is the controller name:
function login_window(){
    // request via ajax
    $this->load->view("login_window");
}

jQuery

jQuery

//here is the jquery code:
//I am using a jquery plugin FACEBOX

$('a[rel*=dialog]').facebox();

<a href="http://localhost/codeigniter/login_window" rel="dialog">Login</a>

I want to check if it is an AJAX request and if not, i will redirect them to homepage. so there's no way they can access the page that is intended only for ajax requests.

我想检查它是否是 AJAX 请求,如果不是,我会将它们重定向到主页。所以他们无法访问仅用于 ajax 请求的页面。

回答by alex

If you are using a library that sends the X-Requested-Withheader, then you can do...

如果您使用的是发送X-Requested-With标头的库,那么您可以执行...

if (strtolower(filter_input(INPUT_SERVER, 'HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest') {
   // I'm AJAX!
}

回答by Dan F.

As of Codeigniter 2.0 it is prefered to use $this->input->is_ajax_request()

从 Codeigniter 2.0 开始,它更倾向于使用 $this->input->is_ajax_request()

回答by aish

In Codeigniter we can use

在 Codeigniter 中,我们可以使用

if(!$this->input->is_ajax_request()){ // check if request comes from an ajax
    redirect(site_url('home'),'refresh'); // if the request is not coming from an ajax redirect to home controller.
}

回答by Abhinav Singh

I think you are basically looking to protect your ajax api's from being accessed directly by the users. You want users to be able to access ajax api's when invoked by your own code (javascript etc) but users should be denied access if they try to directly hit the api.

我认为您基本上是希望保护您的 ajax api 不被用户直接访问。您希望用户在被您自己的代码(javascript 等)调用时能够访问 ajax api,但是如果用户尝试直接点击 api,则应该拒绝他们访问。

If you are still looking for a perfect solution (HTTP_X_REQUESTED_WITH is not always reliable, since your library might not support this. Even it might get stripped off by proxies if user is behind one) try to use crumbsto protect your ajax api's. Crumbs are used for flow validation, which make sure that users access the api's via a pre-defined/pre-decided flow and not directly.

如果您仍在寻找完美的解决方案(HTTP_X_REQUESTED_WITH 并不总是可靠的,因为您的库可能不支持这一点。如果用户落后,即使它可能会被代理剥离)尝试使用crumbs来保护您的 ajax api。Crumbs 用于流验证,确保用户通过预定义/预先确定的流而不是直接访问 api。

回答by Robin Maben

Instead of detecting whether your request was an ajax request or not(Which can be any HTTP verb - GET/POST/HEAD) you may wanna try and add/modify routesto your routes.phpfor specifically handling these scenarios.

与其检测您的请求是否是 ajax 请求(可以是任何 HTTP 动词 - GET/POST/HEAD),您可能想要尝试添加/修改路由routes.php专门处理这些场景。

回答by Fancy John

In Yii you simply check

在 Yii 中,您只需检查

    if (Yii::app()->request->isAjaxRequest)

If you use jQuery or other major javascript library it works. If you do custom requests, don't forget ot set X-Requested-WithHTTP header to XMLHttpRequest.

如果您使用 jQuery 或其他主要的 javascript 库,它可以工作。如果您进行自定义请求,请不要忘记将X-Requested-WithHTTP 标头设置为XMLHttpRequest.

回答by Dheeraj Thedijje

Codeigniter has inbuilt function to check if the request is made using Ajax call.

Codeigniter 具有内置函数来检查请求是否是使用 Ajax 调用发出的。

You can use the following way to validate if a controller/segmentis called using Ajax or not.

您可以使用以下方式来验证是否controller/segment使用 Ajax 调用了 a 。

<?php
Class Only_ajax extends CI_controller{

   function validate_user()
  {
     /*
      * Check if URL only_ajax/validate_url is called from ajax
      * if not display not found error to user.
      *
      **/

     if(!$this->input->is_ajax_request()){
       show_404();
     }

  }

}

You can use many other checks as well using inputclass. Few of them are

您也可以使用input类来使用许多其他检查。他们中很少有人是

  • $this->input->get_request_header();
  • $this->input->is_cli_request()
  • $this->input->ip_address()
  • $this->input->get_request_header();
  • $this->input->is_cli_request()
  • $this->input->ip_address()

You can view complete list of available methods at Official documentation

您可以在官方文档中查看可用方法的完整列表