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()。
//Here is the controller name:
function login_window(){
// request via ajax
$this->load->view("login_window");
}
//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请求的页面。
76
If you are using a library that sends the X-Requested-With
header, then you can do...
如果您正在使用一个发送X-Requested-With header的库,那么您可以…
if (strtolower(filter_input(INPUT_SERVER, 'HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest') {
// I'm AJAX!
}
17
As of Codeigniter 2.0 it is prefered to use $this->input->is_ajax_request()
对于Codeigniter 2.0,最好使用$this->输入->is_ajax_request()
3
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 crumbs to 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并不总是可靠的,因为您的库可能不支持这一点)。即使它可能被代理剥离,如果用户在后面)尝试使用碎屑保护您的ajax api。碎屑用于流验证,它确保用户通过预先定义的/预先确定的流访问api,而不是直接访问。
3
In Codeigniter we can use
在可待因点火器中我们可以使用
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.
}
1
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 routes to your routes.php
for specifically handling these scenarios.
不要检测你的请求是否是ajax请求(它可以是任何HTTP动词- GET/POST/HEAD),你可以尝试添加/修改路由到你的路线。php专门用于处理这些场景。
1
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-With
HTTP header to XMLHttpRequest
.
如果您使用jQuery或其他主要的javascript库,它可以工作。如果您进行自定义请求,不要忘记将带有HTTP报头的x - requestee设置为XMLHttpRequest。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2010/11/29/5c97ae6b2462a85e5378da89a86d012d.html。