我正在用PHP编写代码,需要config.php
根据网站URL 包含文件.
我在变量中有网站URL $site
,而config $site
在目录中configs/$site/config.php
.
如何在PHP中动态地需要此文件?
这样做安全include "configs/$site/config.php";
吗?
如果限制$site
为一组值,则为是.
$sites = array(
'siteA',
'siteB'
);
if (isset($sites[$site])) {
include "configs/$site/config.php";
} else {
throw new Exception("Unknown site");
}
Run Code Online (Sandbox Code Playgroud)
但请记住:永远不要相信来自外部的任何东西,总是验证来自客户端(浏览器)的价值.