Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 限制对文件的直接访问也会拒绝ajax请求_Php_Ajax_.htaccess - Fatal编程技术网

Php 限制对文件的直接访问也会拒绝ajax请求

Php 限制对文件的直接访问也会拒绝ajax请求,php,ajax,.htaccess,Php,Ajax,.htaccess,我想拒绝直接访问所有php文件。因此,我在.htaccess文件中使用以下命令。禁用直接访问可以正常工作,但我可以在index.php中包含任何php文件。但是,当来自index.php的所有ajax请求也被阻止时,问题就会出现。如何取消阻止ajax请求 这是禁止的(如预期): include(other.php) $.ajax({ method: "POST", url: "other.php", dataType: "json", data: JSON.st

我想拒绝直接访问所有php文件。因此,我在.htaccess文件中使用以下命令。禁用直接访问可以正常工作,但我可以在index.php中包含任何php文件。但是,当来自index.php的所有ajax请求也被阻止时,问题就会出现。如何取消阻止ajax请求

这是禁止的(如预期):

include(other.php)
$.ajax({
    method: "POST",
    url: "other.php",
    dataType: "json",
    data: JSON.stringify({ field: field})
})
.done(function( msg ) {
    console.log(msg);
})
.fail(function( jqXHR, textStatus, errorThrown) {
    console.log(errorThrown);
});

这是通过的(如预期):

include(other.php)
$.ajax({
    method: "POST",
    url: "other.php",
    dataType: "json",
    data: JSON.stringify({ field: field})
})
.done(function( msg ) {
    console.log(msg);
})
.fail(function( jqXHR, textStatus, errorThrown) {
    console.log(errorThrown);
});

这将在index.php中进行(如预期的那样):

include(other.php)
$.ajax({
    method: "POST",
    url: "other.php",
    dataType: "json",
    data: JSON.stringify({ field: field})
})
.done(function( msg ) {
    console.log(msg);
})
.fail(function( jqXHR, textStatus, errorThrown) {
    console.log(errorThrown);
});
但这在index.php中也会返回禁止:

include(other.php)
$.ajax({
    method: "POST",
    url: "other.php",
    dataType: "json",
    data: JSON.stringify({ field: field})
})
.done(function( msg ) {
    console.log(msg);
})
.fail(function( jqXHR, textStatus, errorThrown) {
    console.log(errorThrown);
});
.htaccess

<FilesMatch ".*\.php$">
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
    Allow from ::1
</FilesMatch>

<Files index.php>
    Order Allow,Deny
    Allow from all
</Files>

命令拒绝,允许
全盘否定
允许从127.0.0.1开始
允许来自::1
命令允许,拒绝
通融

你不能。您可以将它们分别放在不同的目录中,并将它们命名为index.php,或者您可以使用这些文件创建一个ajax目录,并为ajax中的所有内容设置一个例外。请详细说明一下?你的意思是我不应该限制htaccess直接访问我通过Ajax调用的php文件吗?在这种情况下,我有没有办法拒绝直接访问这些php文件?通过ajax调用的任何文件都必须是可访问的。如果为了直接调用而阻止它,则不能通过ajax调用它,因为ajax是从客户端浏览器进行的直接调用。您试图保护的
other.php
是什么?与其试图阻止直接访问,它应该单独检查
if(!$authenticated){header('HTTP/1.1 401 Unauthorized');exit;}
发出请求的行数,无论是通过浏览器键入的还是您有进行ajax调用的javascript。在这两种情况下,用户对数据的访问权限100%相同。换句话说,即使你只能通过ajax使用它,你也会遇到与他们输入时完全相同的安全问题。如果通过会话检查来保护它,就没有理由阻止访问。只有当会话有效时,用户才能访问。