dragon8002 2015-07-18 02:19
浏览 35
已采纳

php ini vs json和halt编译器hack

I am debating on using either a configuration.ini or config.json for my administration dashboard I am designing. I like that ini is more for configuration purposes but with todays interest in JSON it seems more logical for the configurations be designed with json in mind.

Question here is should I use json for sure? And secondly I have this hack currently on my configuration.ini file which prevents the file being seen publicly.

;<?php exit(); __halt_compiler();
; //to stop script execution if not used in our best interest!
; //remember this format

  //ini stuff

;?>

I was wondering if I could use this with the json file as well? I haven't tested it just because I didn't want nothing strange completely screwing everything up.As well as a good description as to why this works, now I know this won't work specifically with JSON since the ; is ini based commenting so could it be transformed to // or /**/ for json?

For interest purposes this is currently my admin__autoload.php page

set_include_path(dirname($_SERVER["DOCUMENT_ROOT"]));   
$ini = parse_ini_file("configurations.ini",true);
foreach($ini as $section=>$values) {
    foreach($values as $key=>$value )   {
        define("__".strtoupper($key)."__",$value);
    }
}

spl_autoload_register(function($class) {
        if(!file_exists(get_include_path(). DIRECTORY_SEPARATOR .__ADMIN__."classes/{$class}.php")) {
            echo get_include_path(). DIRECTORY_SEPARATOR .__ADMIN__."classes/{$class}.php does not exist";
        } else {
            include_once get_include_path(). DIRECTORY_SEPARATOR .__ADMIN__."classes/{$class}.php";
        }
});

__ADMIN__ was created with the foreach loop from my ini file.

  • 写回答

1条回答 默认 最新

  • duanju8308 2015-07-18 02:41
    关注

    I think you're asking two different questions here.

    1. Which configuration format will best suite my needs
    2. How do I prevent my configuration files from being accessed by the public

    The answer to 2 is easy. Don't put your configuration files in your public document root.

    As far as choosing between INI or JSON, it's rather subjective without understanding the use case very clearly.

    INI is more expressive for the reader but harder to parse. JSON is quite portable as a serialization format, but harder to read as a human.

    So if you're manually editing the configuration file a lot, it makes sense to go with INI as its slightly disambiguated than JSON. If you're doing the editing through an autonomous process or one that otherwise requires portability across disparate systems, JSON may be more convenient.

    A third alternative is YAML, which is a good middle ground between the human-readable aspect of INI, and the portability of JSON.

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c