构建可配置PHP程序的正确方式
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 本文举例说明了创建可配置 PHP 应用程序的几种方法。文中也探讨了应用程序中理想的配置点,并在应用程序过分可配置和过分封闭之间寻求一个平衡点。 如果计划让其他人或公司可以使用您的 PHP 应用程序
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
本文举例说明了创建可配置 PHP 应用程序的几种方法。文中也探讨了应用程序中理想的配置点,并在应用程序过分可配置和过分封闭之间寻求一个平衡点。
如果计划让其他人或公司可以使用您的 PHP 应用程序,需要确保该程序是可配置的。至少,要允许用户以一种安全的方式设置数据库登录及密码,从而使其中的材料不会对外公开。
本文展示了几种用于存储配置设置及编辑这些设置的技术。另外,文中也为哪些元素需要设为可配置以及如何避免陷入配置过度或者配置不足的困境提供了指导。
使用 INI 文件进行配置
PHP 内建了对配置文件的支持。这是通过 php.ini 文件这样的初始化文件(INI)机制实现的,在 php.ini 文件中定义了数据库连接超时或会话如何存储等常量。如果愿意的话,可以在这个 php.ini 文件中为应用程序定制配置。为了说明,我将下列代码行添加到 php.ini 文件中。
myapptempdir=foo
然后,我编写了一个小 PHP 脚本来读取这个配置项,如清单 1 所示。
清单 1. ini1.php
function get_template_directory()
{
$v = get_cfg_var( "myapptempdir" );
return ( $v == null ) ? "tempdir" : $v;
}
echo( get_template_directory()。"\n" );
?>
当在命令行中运行这段代码时,得到如下结果:
% php ini1.php
foo
%
太棒了。但为什么不能用标准的 INI 函数来获取 myapptempdir 配置项的值呢?我研究了一下,发现在大多数情况下,定制配置项不能使用这些方法来获取。然而,使用 get_cfg_var 函数却是可以访问的。
为使这个方法更加简单,将对变量的访问封装在第二个函数中,该函数使用配置键名及一个缺省值作为参数,如下所示。
清单 2. ini2.php
function get_ini_value( $n, $dv )
{
$c = get_cfg_var( $n );
return ( $c == null ) ? $dv : $c;
}
function get_template_directory()
{
return get_ini_value( "myapptempdir", "tempdir" );
}
这是对如何访问 INI 文件的一个很好的概括,所以,如果要使用一个不同的机制或将这个 INI 文件存储到其他位置,就不需要为更改大量的函数而大费周折。
我不推荐使用 INI 文件作为应用程序的配置,这有两个理由。首先,虽然这样做较容易读取 INI 文件,但却几乎不可能安全地写 INI 文件。所以这样做只适合于只读配置项。第二,php.ini 文件在服务器的所有应用程序上共享,所以我认为特定于应用程序的配置项不应该写在该文件中。
需要对 INI 文件了解什么呢?最重要的是如何重置 include 路径来添加配置项,如下所示。
清单 3. ini3.php
echo( ini_get("include_path")。"\n" );
ini_set("include_path",
ini_get("include_path")。":./mylib" );
echo( ini_get("include_path")。"\n" );
?>
在本例中,我将我的本地 mylib 目录添加到了 include 路径中,所以能够从该目录中 require PHP 文件,而不需要将该路径添加到 require 语句中。
PHP 中的配置
通常对于在 INI 文件中存储配置条目的一个替代办法是使用一个简单的 PHP 脚本来保持数据。如下是一个样例。
[1] [2] [3] [4]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...
