1

我正在创建一个允许用户安装不同主题的 php 应用程序。每个主题目录都必须放在“主题”目录中。每个主题必须有一个名为的文件,该文件theme_profile.php具有主题规范,例如主题名称等...

所以这个 theme_profile.php 有一堆变量包装在一个名为 $theme 的数组中,($theme["title"]="My theme"等等......)

现在我希望我的应用程序索引文件在不简单地“包含”文件的情况下获取这些变量(如果将恶意代码放置在此文件中,这是有风险的)。

有没有什么之类的get_variables_from_file("file.php")

4

3 回答 3

4

没有必要为此目的使用 php 文件。只需创建 XML 或任何其他数据文件类型并在 PHP 代码中解析它们。这要安全得多。

于 2012-05-16T00:11:14.880 回答
0

Having a look at this answer on another question may send you in the right direction : https://stackoverflow.com/a/1858294/303657

于 2012-05-16T00:25:27.400 回答
0

如其他答案所述,在这种情况下不需要使用 PHP。实际上最好使用数据文件。(如 XML、JSON 等)

这是一个示例 JSON 编码文件:

// first create the $theme array
$theme['title'] = '...';
$theme['...'] = '...';

// then json encode it
$themeJson = json_encode($theme);

// put it in a file
file_put_contents('theme_profile.json', $themeJson);

出于加载目的,只需使用json_decode 函数

您也可以以相同的方式使用PHP 序列化。

于 2016-04-04T16:39:23.677 回答