Home php教程 php手册 构建可配置PHP应用程序的正确方式

构建可配置PHP应用程序的正确方式

May 25, 2016 pm 04:53 PM
app Construct Configuration

本文举例说明了创建可配置 PHP 应用程序的几种方法。文中也探讨了应用程序中理想的配置点,并在应用程序过分可配置和过分封闭之间寻求一个平衡点。

假如计划让其他人或公司可以使用您的 PHP 应用程序,需要确保该程序是可配置的。至少,要答应用户以一种安全的方式设置数据库登录及密码,从而使其中的材料不会对外公开。

本文展示了几种用于存储配置设置及编辑这些设置的技术。另外,文中也为哪些元素需要设为可配置以及如何避免陷入配置过度或者配置不足的困境提供了指导。

使用 INI 文件进行配置

PHP 内建了对配置文件的支持。这是通过 php.ini 文件这样的初始化文件(INI)机制实现的,在 php.ini 文件中定义了数据库连接超时或会话如何存储等常量。假如愿意的话,可以在这个 php.ini 文件中为应用程序定制配置。为了说明,我将下列代码行添加到 php.ini 文件中。

myapptempdir=foo

然后,我编写了一个小 PHP 脚本来读取这个配置项,如清单 1 所示。

清单 1. ini1.php

<?php
function get_template_directory() {
      $v = get_cfg_var("myapptempdir");
     return($v == null) ? "tempdir" : $v;
}
echo (get_template_directory() . "\n");
?>
Copy after login

当在命令行中运行这段代码时,得到如下结果:

% php ini1.php 
foo 
%
Copy after login

太棒了。但为什么不能用标准的 INI 函数来获取 myapptempdir 配置项的值呢?我研究了一下,发现在大多数情况下,定制配置项不能使用这些方法来获取。然而,使用 get_cfg_var 函数却是可以访问的。

为使这个方法更加简单,将对变量的访问封装在第二个函数中,该函数使用配置键名及一个缺省值作为参数,如下所示。

清单 2. ini2.php

<?php
function get_ini_value($n, $dv) {
      $c = get_cfg_var($n);
     return($c == null) ? $dv : $c;
}
function get_template_directory() {
     returnget_ini_value("myapptempdir", "tempdir");
}
?>
Copy after login

这是对如何访问 INI 文件的一个很好的概括,所以,假如要使用一个不同的机制或将这个 INI 文件存储到其他位置,就不需要为更改大量的函数而大费周折。

我不推荐使用 INI 文件作为应用程序的配置,这有两个理由。首先,虽然这样做较轻易读取 INI 文件,但却几乎不可能安全地写 INI 文件。所以这样做只适合于只读配置项。第二,php.ini 文件在服务器的所有应用程序上共享,所以我认为特定于应用程序的配置项不应该写在该文件中。

需要对 INI 文件了解什么呢?最重要的是如何重置 include 路径来添加配置项,如下所示。

清单 3. ini3.php

<?php
echo (ini_get("include_path") . "n");
ini_set("include_path", ini_get("include_path") . ":./mylib");
echo (ini_get("include_path") . "n");
?>
Copy after login

在本例中,我将我的本地 mylib 目录添加到了 include 路径中,所以能够从该目录中 require PHP 文件,而不需要将该路径添加到 require 语句中。

PHP 中的配置

通常对于在 INI 文件中存储配置条目的一个替代办法是使用一个简单的 PHP 脚本来保持数据。如下是一个样例。

清单 4. config.php

<?php
// Specify the location of the temporary directory
//
$TEMPLATE_DIRECTORY = "tempdir";
?>
Copy after login

使用该常量的代码如上所示。


               
               

文章网址:

随意转载^^但请附上教程地址。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
Unable to save changes to Photos app error in Windows 11 Unable to save changes to Photos app error in Windows 11 Mar 04, 2024 am 09:34 AM

If you encounter the Unable to save changes error while using the Photos app for image editing in Windows 11, this article will provide you with solutions. Unable to save changes. An error occurred while saving. Please try again later. This problem usually occurs due to incorrect permission settings, file corruption, or system failure. So, we’ve done some deep research and compiled some of the most effective troubleshooting steps to help you resolve this issue and ensure you can continue to use the Microsoft Photos app seamlessly on your Windows 11 device. Fix Unable to Save Changes to Photos App Error in Windows 11 Many users have been talking about Microsoft Photos app error on different forums

How to connect Apple Vision Pro to PC How to connect Apple Vision Pro to PC Apr 08, 2024 pm 09:01 PM

The Apple Vision Pro headset is not natively compatible with computers, so you must configure it to connect to a Windows computer. Since its launch, Apple Vision Pro has been a hit, and with its cutting-edge features and extensive operability, it's easy to see why. Although you can make some adjustments to it to suit your PC, and its functionality depends heavily on AppleOS, so its functionality will be limited. How do I connect AppleVisionPro to my computer? 1. Verify system requirements You need the latest version of Windows 11 (Custom PCs and Surface devices are not supported) Support 64-bit 2GHZ or faster fast processor High-performance GPU, most

MS Paint not working properly in Windows 11 MS Paint not working properly in Windows 11 Mar 09, 2024 am 09:52 AM

Microsoft Paint not working in Windows 11/10? Well, this seems to be a common problem and we have some great solutions to fix it. Users have been complaining that when trying to use MSPaint, it doesn't work or open. Scrollbars in the app don't work, paste icons don't show up, crashes, etc. Luckily, we've collected some of the most effective troubleshooting methods to help you resolve issues with Microsoft Paint app. Why doesn't Microsoft Paint work? Some possible reasons why MSPaint is not working on Windows 11/10 PC are as follows: The security identifier is corrupted. hung system

The working principle and configuration method of GDM in Linux system The working principle and configuration method of GDM in Linux system Mar 01, 2024 pm 06:36 PM

Title: The working principle and configuration method of GDM in Linux systems In Linux operating systems, GDM (GNOMEDisplayManager) is a common display manager used to control graphical user interface (GUI) login and user session management. This article will introduce the working principle and configuration method of GDM, as well as provide specific code examples. 1. Working principle of GDM GDM is the display manager in the GNOME desktop environment. It is responsible for starting the X server and providing the login interface. The user enters

Shazam app not working in iPhone: Fix Shazam app not working in iPhone: Fix Jun 08, 2024 pm 12:36 PM

Having issues with the Shazam app on iPhone? Shazam helps you find songs by listening to them. However, if Shazam isn't working properly or doesn't recognize the song, you'll have to troubleshoot it manually. Repairing the Shazam app won't take long. So, without wasting any more time, follow the steps below to resolve issues with Shazam app. Fix 1 – Disable Bold Text Feature Bold text on iPhone may be the reason why Shazam is not working properly. Step 1 – You can only do this from your iPhone settings. So, open it. Step 2 – Next, open the “Display & Brightness” settings there. Step 3 – If you find that “Bold Text” is enabled

Understand Linux Bashrc: functions, configuration and usage Understand Linux Bashrc: functions, configuration and usage Mar 20, 2024 pm 03:30 PM

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

Move photos from old Photos to the new Photos app in Windows 11 Move photos from old Photos to the new Photos app in Windows 11 Mar 10, 2024 am 09:37 AM

This article will guide you on how to migrate photos from Photos Legacy to the new Photos app in Windows 11. Microsoft has introduced a revamped Photos app in Windows 11, giving users a simpler and more feature-rich experience. The new Photos app sorts photos differently than the past PhotosLegacy app. It organizes photos into folders like other Windows files instead of creating albums. However, users still using the Photos Legacy app can easily migrate their photos to the new version of Microsoft Photos. What is Phot

Why does 0xc0000142 error code appear? Why does 0xc0000142 error code appear? Feb 25, 2024 am 11:15 AM

0xc0000142 refers to the error code in Windows systems, and it is usually related to the problem of a program or application not starting or running properly. When a user tries to open a program, the system displays the error message "0xc0000142" and the program cannot continue to run. So, why does the 0xc0000142 error occur? There are many reasons for this error code, here are some common causes and solutions. Corrupted configuration files: The configuration files of some programs may be corrupted, causing

See all articles