Php 防止通过AJAX调用直接访问文件
我在这个问题上伤了脑筋,我想阻止人们直接访问我使用AJAX调用的文件 简而言之:Php 防止通过AJAX调用直接访问文件,php,jquery,ajax,Php,Jquery,Ajax,我在这个问题上伤了脑筋,我想阻止人们直接访问我使用AJAX调用的文件 简而言之: main script.php对AJAX request.php 只能从main\u script.php 阻止直接访问ajax request.php 我已经读过类似的问题,比如,但是这里的“被接受的答案”(就像许多其他答案一样)似乎不应该被接受。然而,引起我注意的是,他说的是使用$\u SESSION和散列。现在的问题是(1)在这种情况下,我怀疑你是否真的能阻止人们直接访问;(2) 我不知道你是如何使用会话
对main script.php
AJAX request.php
- 只能从
main\u script.php
- 阻止直接访问
ajax request.php
$\u SESSION
和散列。现在的问题是(1)在这种情况下,我怀疑你是否真的能阻止人们直接访问;(2) 我不知道你是如何使用会话和散列来实现这一点的
因此,如果有人能帮助我思考,或者给我一个正确的方向(或者在根本不可能的情况下给我建议),我将不胜感激。你不能阻止我请求
yourdomain/ajax request.php
,只要我知道那里有一些资源,但是,您可以防止未经授权访问此文件,类似于防止来宾访问您网站上的成员区域或任何需要授权的资源。区别在于,我没有通过提供密码获得授权访问权限,不,只有在我请求main\u script.php
实现这一点的最简单方法是使用($\u COOKIE
和数据库)或($\u SESSION
),例如
main_script.php
$_SESSION['canRequestAjax'] = 1;
unset($_SESSION['canRequestAjax']);
/*
remove the variable if he visited something else,
because he can only request the ajax-request.php RIGHT AFTER the main_script.php
*/
if (empty($_SESSION['canRequestAjax'])){
die('error'); // TODO: proper 403 response
}
unset($_SESSION['canRequestAjax']);
/*
remove the variable if he requested the ajax ajax-request.php file, only 1 ajax is allowed.
NOTE: remove this line if the user may do more than 1 ajax requests when he is on
main-script.php
*/
其他脚本.php
$_SESSION['canRequestAjax'] = 1;
unset($_SESSION['canRequestAjax']);
/*
remove the variable if he visited something else,
because he can only request the ajax-request.php RIGHT AFTER the main_script.php
*/
if (empty($_SESSION['canRequestAjax'])){
die('error'); // TODO: proper 403 response
}
unset($_SESSION['canRequestAjax']);
/*
remove the variable if he requested the ajax ajax-request.php file, only 1 ajax is allowed.
NOTE: remove this line if the user may do more than 1 ajax requests when he is on
main-script.php
*/
ajax request.php
$_SESSION['canRequestAjax'] = 1;
unset($_SESSION['canRequestAjax']);
/*
remove the variable if he visited something else,
because he can only request the ajax-request.php RIGHT AFTER the main_script.php
*/
if (empty($_SESSION['canRequestAjax'])){
die('error'); // TODO: proper 403 response
}
unset($_SESSION['canRequestAjax']);
/*
remove the variable if he requested the ajax ajax-request.php file, only 1 ajax is allowed.
NOTE: remove this line if the user may do more than 1 ajax requests when he is on
main-script.php
*/
为什么你不能允许直接访问?你不能这样做。“你所能做的一切都会让它变得更难。”“好吧,我只是不想让它太容易刮取数据。”。我已经考虑过利率限制,但这更多的是一种变通办法,而不是实际的解决方案。这是绝对可能的。但是,为了防止直接访问基本上是通过公共请求直接访问的文件,我们做了很多努力。在最简单的形式中,您可以只检查ajax请求是否未访问它(只需在浏览器中加载URL就会返回您给出的任何错误),或者,在两个文件之间提供标记化的通信形式。无论哪种方式,都可以做到,但我不需要详细解释。
X-request-With
(或设置另一个自定义头),确保使用用户名和密码或有效会话对请求进行身份验证可能是最接近的。问题是,通过取消设置cookie,其他php脚本将需要再次设置cookie,这仅在运行时发生。因此,任何后续的ajax请求都将失败。这是一个半心半意的努力。@提示这是有意的,OP需要允许访问ajax request.php
只有当用户在main_script.php
上时,例如:用户请求main_script.php
然后导航到其他地方,他不能发出ajax请求,我认为这就是OP想要的。OP的一个评论是“我已经考虑过利率限制,但这更多的是一个变通办法,而不是一个实际的解决方案”。此解决方案在一定程度上限制了速率。@Cue-hmmm,因此OP需要阻止机器人请求此资源!这是另一个故事,我回答了如何使ajax request.php
只能从main_script.php
访问的问题?顺便说一句,这个解决方案与速率限制无关,scraper将被编程为直接请求1-main_script.php
2-ajax request.php
,而不是1-ajax request.php
,没有任何限制,他应该搜索防止机器人程序,这是一个问题。这里的问题是,如果我访问main script.php
一次并保存cookie,我也可以为cookie提供请求ajax request.php
,这仍然太简单了。这实际上不是机器人的问题(从来没有说过),更像是“每个人都应该被阻止直接访问,并且只有当请求是来自main script.php
的AJAX调用时,它才应该工作”。所以您的解决方案只会在一定程度上起作用,假设来宾在此之前没有访问main_script.php
。