PHP配置文件读写

发布于 2024-11-02 17:40:41 字数 1005 浏览 3 评论 0 原文

可能的重复:
读取和写入配置文件

好的,我有一个配置文件,其中包含一些信息我希望用户能够更改。我已经考虑过将信息保存在 MySQL 表中,但这不是一个好主意,因为我在配置变量之后包含了数据库类,这会弄乱我的代码结构。
知道如何读取/写入文件的变量/特定行吗?这是我的全局配置文件。>

error_reporting(E_ALL);

/*
    The parameters used site-wide
    First set are Datbase parameters
    And second set are the salts used to generate strings
*/
$params['db']['host'] = 'localhost';
$params['db']['username'] = 'root';
$params['db']['password'] = 'waffliner';
$params['db']['db_name'] = 'habcms';

$params ['core']['salt1'] = "184962574320355793361124913390";
$params['core']['salt2'] = "13952134426315134482857516025";

$params['core']['timeout'] = "60 minutes";


try {
    include_once("core.inc.php");
    include_once("db.inc.php");
    include_once("user.inc.php");       
} catch(exception $e) {
    echo $e->getMessage();
}

?>

Possible Duplicate:
Reading and Writing Configuration Files

Okay, I have a config file, which contains some information that I would like the user to be able to change. I've already thought about holding the information in a MySQL table but this isn't a good idea as I've got my Database class included AFTER my configuration variables and this would mess up my code structure.
Any idea how to read/write variables/specific lines of a file? Here's my global config file.>

error_reporting(E_ALL);

/*
    The parameters used site-wide
    First set are Datbase parameters
    And second set are the salts used to generate strings
*/
$params['db']['host'] = 'localhost';
$params['db']['username'] = 'root';
$params['db']['password'] = 'waffliner';
$params['db']['db_name'] = 'habcms';

$params ['core']['salt1'] = "184962574320355793361124913390";
$params['core']['salt2'] = "13952134426315134482857516025";

$params['core']['timeout'] = "60 minutes";


try {
    include_once("core.inc.php");
    include_once("db.inc.php");
    include_once("user.inc.php");       
} catch(exception $e) {
    echo $e->getMessage();
}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2024-11-09 17:40:41

JSON♥ 是您的 朋友

{
    "db": {
        "host" : "localhost",
        "username" : "root"
        ...
    },
    ...
}

JSON♥ is your friend.

{
    "db": {
        "host" : "localhost",
        "username" : "root"
        ...
    },
    ...
}
你没皮卡萌 2024-11-09 17:40:41

如果您想这样做,您需要将配置存储在非 PHP 文件中。

例如,您可以将配置存储在 .ini 文件中,并使用 parse_ini_file().

If you want to do so, you need to store you configuration in a non-PHP file.

You can, for example, store your config in an .ini file and read it with parse_ini_file().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文