在PHP中存储配置变量的最佳方法是什么? [英] What is the best way to store configuration variables in PHP?

查看:90
本文介绍了在PHP中存储配置变量的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在PHP中存储一堆配置信息.

I need to store a bunch of configuration information in PHP.

我考虑了以下....

I have considered the following....

// 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';
}     

这是我到目前为止所想到的.我打算使用require /config.inc.php将此配置信息导入我的应用程序.

This is all I have thought of so far. I intend to import this configuration information into my application with require /config.inc.php.

关于存储配置数据,什么对您有用,与此相关的最佳实践是什么?

What works for you with regard to storing configuration data, and what are best practices concerning this?

推荐答案

我一直选择选项#2,并确保除了所有者之外,没有其他人可以使用它.这是Joomla,vBulletin,Gallery等众多PHP应用程序中最受欢迎的方法.

I've always gone with option #2 and just ensure that no one but the owner has ANY sort of access to it. It's the most popular method among PHP applications like Joomla, vBulletin, Gallery, and numerous others.

第一种方法对我来说太乱了(可读性),第三种方法太危险了.我从没考虑过Class方法,因此其他人可以在该方法上提供他们的输入.但是我想,只要在​​类的用法上使用正确的访问权限就可以了.

First method is too messy to me (readability) and the third is WAY too dangerous to do. I've never thought about the Class method, so someone else can provide their input on that one. But I guess it's fine so long as the right access is used on the class' usage.

示例..

define('EXAMPLE1', "test1"); // scenario 1
$example2 = "test2"; // scenario 2

function DealWithUserInput($input)
{
   return eval($input);
}

现在,此代码示例确实很愚蠢,但仅是一个示例.考虑该函数可能返回的内容,具体取决于用户可以在输入中使用的场景.

Now this example of code is really dumb, but just an example. Consider what could be returned by the function depending on which scenario the user could try to use in their input.

仅当您在函数内将其设置为全局时,方案2才会引起问题.否则,它将超出范围且无法访问.

Scenario 2 would only cause an issue if you made it a global within the function. Otherwise it's out of scope and unreachable.

这篇关于在PHP中存储配置变量的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆