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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分