存储全局设置,可从 PHP 的管理页面进行配置 [英] Storing global settings, configurable from an admin page in PHP

查看:23
本文介绍了存储全局设置,可从 PHP 的管理页面进行配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找能够从管理面板设置一些基本的站点范围设置(可能是布尔值或只是小数字)的最有效方法,并在每个页面上检索它们(通过公共文件)并存储在 PHP 变量中.

I'm looking for the most efficient way to be able to set some basic site-wide settings (probably in boolean or just small numbers) from an admin panel, and have them retrieved on every page (via a common file) and stored in PHP variables.

具体来说,我的网站上有一个积分系统,我希望其他网站经理能够设置奖励和成本,并将它们存储在每个页面的变量中.

Specifically, I have a points system on my site, and I'd like to be able to have other site managers be able to set the rewards and costs, and have them be already stored in a variable on every page.

研究一个流行的论坛软件如何存储设置,他们使用了一个包含行 idnamevalue 的 MySQL 表.Name 是选项的名称,value 是选定的值.我没有研究如何在代码本身中检索它,以及是否在每次运行公共/头文件时检查它.在我看来,在每个页面上针对很少更改的设置进行 MySQL 查询的效率不会很高.

Looking into how a popular forum software had stored settings, they used a MySQL table with rows id, name, and value. Name being the name of the option, and value being the selected value. I didn't look into how this was retrieved in the code itself though, and if it was checked on every run of the common/header file. It seemed to me that having a MySQL query on every page for settings that rarely change wouldn't be very efficient.

我考虑的另一件事是有一个文本文件,它只将选项存储在 JSON 中.

Another thing I considered was having a text file that just stored the options in JSON.

让我知道这些想法中的一个是否可行,或者是否有更理想的想法.我对任何事情都持开放态度.

Let me know if one of these ideas would work, or if there is something that would be more ideal. I'm open to anything.

推荐答案

存储应用程序范围的配置设置确实存在对数据库的误用.然而,性能损失本身通常可以忽略不计.

Storing application-wide configuration settings there is really a misuse of the database. While the performance penalty itself is usually negligible however.

最有效的方法当然是数据文件.除了 json_encode,您还可以使用 serialize 或生成一个有效的 .php 脚本:

The most efficient approach is certainly a data file. Instead of json_encode you might as well use serialize or generate a valid .php script with just:

file_put_contents("config.php",
     '<?php $config = ' . var_export($config_array, 1) . '; ?>');

如果您只需要一些设置,那么简单的管理 ui 的工作量同样很小.

The effort for a simple admin ui is likewise small, if you only need a few settings.

关于过度使用的解决方案,请参见 http://milki.include-once.org/genericplugins/genconfig.html

这篇关于存储全局设置,可从 PHP 的管理页面进行配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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