dqoys62082 2015-01-05 05:07
浏览 74
已采纳

安全地提取目录内容将PHP和使用JQuery读取它们?

My question is about PHP security for this particular operation:

I want to use javascript to pull all json files from a particular directory on my web server. I have done like so: I pull all the correct file names with this PHP script ("get-data.php"):

<?php
echo json_encode(glob('*.json'));
?>

Then I move that array into javascript with

var oReq = new XMLHttpRequest();
oReq.onload = function() {
    fileNames = JSON.parse(this.responseText);
};
oReq.open("get", "get-data.php", true);
oReq.send();

Then I use the following to read the files into an array:

function getMapInfo(fileName){
    $.get(fileName, function(result) {
        var map = JSON.parse(result);
        mapData.push(map);
    });
 }

I got some of this code from here: How to pass variables and data from PHP to JavaScript? and I have read up on xss a little here and here and it is my understanding that "untrusted data" is data that the user would enter which would then be run in a script? I believe that my above solution doesn't contain unsafe data since it only pulls files that are already on my server, is that correct?

Overall, my question is: is this a safe way to allow my code to retrieve multiple unknown files from my server? Eventually, I want uses to "save" map data to the server which will then be read by the above script for others to see.

Thanks very much,

Jordan

  • 写回答

1条回答 默认 最新

  • doutan1671 2015-01-13 17:14
    关注

    Yes, this is perfectly safe. You will just need to ensure the security is part of the php code when needed, by limiting or filtering what it can select (already fine there) and how .json files are validated and stored, once that is addressed you will be fine and your existing solution is perfectly safe. You can also modify .htaccess file to hide folder content if you have a concern about others viewing directories on your website.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算