dongxi4335 2010-03-06 12:24
浏览 35
已采纳

如果生成错误,则禁用文件包含(在php中)

I would like to implement a simple plugin system for my script. I'll be including the files of the plugins which the user selects as available.

Currently, if any of the files has some parse error, it causes the complete thing to go down, until I hack into the db to remove that plugin entry.

Is there any easy way to do this? (Somehow check the files for parse errors atleast)?

  • 写回答

2条回答 默认 最新

  • douganmo1121 2010-03-06 12:28
    关注

    You could make a HTTP request to a PHP file that tries to include the file, and outputs "OK" at the end.

    Check_include.php:

    $filename = $_GET["plugin"];
    
    $filename = sanitize_filename($filename); // Here you make sure only plugins can be loaded
    
    include ($filename);
    echo "OK";
    

    then, in your admin panel or whatever, call

    file_get_contents("http://mydomain/check_include.php?plugin=my_new_plugin");
    

    and see whether the result contains the word "OK". If the result is okay, there was no fatal error, and you can activate the plugin; otherwise, you can even output the error message that got output when trying to include it.

    WordPress uses a variation of this involving an IFRAME to check new plugins.

    Note: Making a HTTP request is expensive. You should under no circumstances do this every time the plugin is included, but only once on installation. Use a flag of some sort (e.g. a underscore _ in front of the plugin name) to enable / disable plugins.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?