阻止直接访问PHP文件,并允许JSON [英] Block direct access to PHP files and allow json

查看:111
本文介绍了阻止直接访问PHP文件,并允许JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想屏蔽掉了PHP脚本的一些文件,不过,我想我的JavaScript的AJAX调用允许脚本,我不知道这甚至有可能,但...

I'm trying to block out some files with a php script, however, i want my javascript ajax calls to allow the scripts, i don't know if this is even possible but..

我现在要做的就是,

$filename = array('index.php');

$basename = basename($_SERVER['REQUEST_URI']);

if(!in_array($basename, $filename)) {
    die('...');
}

这将阻止所有文件,而不是index.php的,但如果我有一个的login.php,使我的Ajax调用可能吗?

This will block all files and not index.php, but what if i have an login.php that makes my ajax calls possible?

推荐答案

当你发送一个JavaScript AJAX调用它增加了

When you send a JavaScript AJAX call it adds

X-要求,通过:XmlHTT prequest

X-Requested-With : XmlHTTPRequest

要在HTTP标头。所以,如果你想要做的事的情况下,一个AJAX的呼叫时,可以 检查是这样的:

To the HTTP headers. So if you want to do something in case of an AJAX call you can check for something like this:

$headers = getallheaders();
if($headers['X-Requested-With') == 'XMLHttpRequest') {
    // ...
}

请记住,任何HTTP客户端可以修改标题,所以它并没有真正增加任何安全(但如浏览器不能直接使用默认设置调用你的PHP脚本)。

Keep in mind that any HTTP client can modify headers, so it doesn't really add any security (but e.g. a browser couldn't call your PHP scripts directly with the default settings).

这篇关于阻止直接访问PHP文件,并允许JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆