ale*_*lex 24 php configuration-files
我需要在PHP中存储一堆配置信息.
我考虑过以下......
// Doesn't seem right.
$mysqlPass = 'password';
// Seems slightly better.
$config = array(
'mysql_pass' => 'password'
);
// Seems dangerous having this data accessible by anything. but it can't be
// changed via this method.
define('MYSQL_PASSWORD', 'password');
// Don't know if this is such a good idea.
class Config
{
const static MYSQL_PASSWORD = 'password';
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我所想到的.我打算将此配置信息导入到我的应用程序中require /config.inc.php.
对于存储配置数据有什么用处,以及有关此问题的最佳做法是什么?
我总是选择#2选项,只是确保除了拥有者之外没有人可以访问它.它是Joomla,vBulletin,Gallery等众多PHP应用程序中最受欢迎的方法.
第一种方法对我来说太麻烦了(可读性),第三种方法太危险了.我从来没有想过Class方法,所以其他人可以提供他们的输入.但是我觉得只要在课堂上使用正确的访问权限就可以了.
例..
define('EXAMPLE1', "test1"); // scenario 1
$example2 = "test2"; // scenario 2
function DealWithUserInput($input)
{
return eval($input);
}
Run Code Online (Sandbox Code Playgroud)
现在这个代码示例真的很愚蠢,但只是一个例子.考虑函数可以返回的内容,具体取决于用户可以尝试在其输入中使用的场景.
如果您在函数中将其设置为全局,则场景2只会导致问题.否则它超出范围且无法访问.