Php 简明教程

PHP.INI File Configuration

在计算机上安装 PHP 软件后,就会在安装目录中创建 php.ini。对于 XAMPP,php.ini 位于 c:\xamm\php 文件夹中。这是一个重要的配置文件,它控制着性能并设置所有与 PHP 相关的参数。

On installing PHP software on your machine, php.ini is created in the installation directory. In case of XAMPP, php.ini is found in c:\xamm\php folder. It is an important configuration file that controls the performance and sets all the PHP related parameters.

phpinfo() 函数显示一个不同的参数列表及其 PHP、Aache、MySQL 和 Web 服务器安装的其他部分的当前值。

The phpinfo() function displays a list of different parameters and their current values of PHP, Aache, MySQL and other parts of the web server installation.

运行以下代码以显示设置,其中一个设置显示“php.ini”文件的路径:

Run the following code to display the settings, one of which shows the path to the "php.ini" file:

<?php
   echo phpinfo();
?>

Loaded Configuration File

查找指明php.ini 文件所在位置的已加载配置文件设置

Locate the Loaded Configuration File setting that displays the location of php.ini file

C:\xampp\php\php.ini

PHP 行为的不同方面由大量的参数(称为指令)配置。“php.ini”文件以分号(;)符号开头的多数行开头 - 表示该行已注释。未注释的行实际上是有效的指令及其值。换句话说,要激活并为特定指令指定值,请删除前导分号。

Different aspects of PHP’s behaviour are configured by a large number of parameters (called directives). The "php.ini" file comes with most of the lines starting with semicolon (;) symbol – indicating that the line is commented. The uncommented line is actually the effective directive and its value. In other words, to activate and assign a value to a particular directive, remove the leading semicolon.

directive = value

指令名称区分大小写。指令是在变量中用于配置 PHP 或 PHP 扩展。请注意,没有名称验证,因此如果没有找到预期的指令,将使用默认值,它可以是字符串、数字、PHP 常量(例如 E_ALL 或 M_PI)、其中一个 INI 常量(开、关、真、假、是、否和无)。

Directive names are *case sensitive. Directives are variables used to configure PHP or PHP extensions. Note that there is no name validation, so if an expected directive is not found a default value will be used, which can be a string, a number, a PHP constant (e.g. E_ALL or M_PI), one of the INI constants (On, Off, True, False, Yes, No and None).

实际上,C:\XAMPP\PHP 文件夹包含两个 INI 文件,一个用于生产环境,另一个用于开发环境。

Actually, the C:\XAMPP\PHP folder contains two INI files, one to be used in production environment and other in development environment.

php.ini-development.ini 与它的生产版本非常相似,除了在错误方面更加详细。在开发阶段,将其复制为 php.ini 以便能够跟踪代码中的错误。一旦代码准备就绪,可以使用 php.ini-production.ini 文件作为有效的 php.ini 文件,这在很大程度上抑制了错误消息。

The php.ini-development.ini is very similar to its production variant, except it is much more verbose when it comes to errors. In development stage, copy this as php.ini to be able to trace the bugs in the code. Once the code is ready for deployment, use php.ini-production.ini file as the effective php.ini file, which essentially supress the error messages to a large extent.

php.ini 中的指令分为不同的类别,如错误处理、数据处理、路径和目录、文件上传、PHP 扩展和模块设置。

The directives in php.ini are divided in different categories, like Error handling, data handling, path and directories, file uploads, PHP extensions and module settings.

以下是“php.ini”文件中一些重要指令的列表:

Here is a list of some of the important directives in "php.ini" file:

short_open_tag = Off

简写标签看起来像这样:<? ?>。如果您想要使用 XML 功能,则必须将此选项设置为“关”。

Short open tags look like this: <? ?>. This option must be set to Off if you want to use XML functions.

safe_mode = Off

如果设置为开,您可能使用 --enable-safe-mode 标志编译了 PHP。安全模式最适用于 CGI 用途。请参见本章前面的“CGI 编译时选项”部分中的说明。

If this is set to On, you probably compiled PHP with the --enable-safe-mode flag. Safe mode is most relevant to CGI use. See the explanation in the section "CGI compile-time options". earlier in this chapter.

safe_mode_exec_dir = [DIR]

此选项仅在安全模式开启时才相关;它也可以在 Unix 构建过程中使用 --with-exec-dir 标志设置。处于安全模式的 PHP 仅会从该目录中执行外部二进制文件。默认值为 /usr/local/bin。这与提供正常的 PHP/HTML 网页无关。

This option is relevant only if safe mode is on; it can also be set with the --with-exec-dir flag during the Unix build process. PHP in safe mode only executes external binaries out of this directory. The default is /usr/local/bin. This has nothing to do with serving up a normal PHP/HTML Web page.

safe_mode_allowed_env_vars = [PHP_]

此选项设置用户在安全模式下可以更改的环境变量。默认值仅为以“PHP_”为前缀的变量。如果此指令为空,则大部分变量均可更改。

This option sets which environment variables users can change in safe mode. The default is only those variables prepended with "PHP_". If this directive is empty, most variables are alterable.

safe_mode_protected_env_vars = [LD_LIBRARY_PATH]

此选项设置用户在安全模式下不可更改的环境变量,即使 safe_mode_allowed_env_vars 设置为宽松的模式。

This option sets which environment variables users can’t change in safe mode, even if safe_mode_allowed_env_vars is set permissively

disable_functions = [function1, function2…​]

PHP4 配置的一个受欢迎的补充,也是在 PHP5 中延续的一个补充,就是出于安全性考虑禁用选定功能的能力。以前,这需要手动编辑 PHP 所基于的 C 代码。文件系统、系统和网络功能可能应该首先启用,因为允许通过 HTTP 编写文件和更改系统从来都不是一个安全的想法。

A welcome addition to PHP4 configuration and one perpetuated in PHP5 is the ability to disable selected functions for security reasons. Previously, this necessitated hand-editing the C code from which PHP was made. Filesystem, system, and network functions should probably be the first to go because allowing the capability to write files and alter the system over HTTP is never such a safe idea.

max_execution_time = 30

函数 set_time_limit() 在安全模式下不可用,因此这是在安全模式下使脚本超时的主要方法。在 Windows 中,您必须根据消耗的最大内存而非时间中止执行。如果您使用 Apache,也可以使用 Apache 超时设置来超时,但这也会应用于网站上的非 PHP 文件。

The function set_time_limit() won.t work in safe mode, so this is the main way to make a script time out in safe mode. In Windows, you have to abort based on maximum memory consumed rather than time. You can also use the Apache timeout setting to timeout if you use Apache, but that will apply to non-PHP files on the site too.

error_reporting = E_ALL & ~E_NOTICE

默认值为 E_ALL & ~E_NOTICE,所有错误,不包括通知。开发服务器应至少设置为默认值;仅正式服务器才能考虑使用较低的值。

The default value is E_ALL & ~E_NOTICE, all errors except notices. Development servers should be set to at least the default; only production servers should even consider a lesser value

error_prepend_string = [""]

与它的配套设置 error_append_string 一起使用时,此设置允许您使错误消息与其他文本的不同颜色,或满足您的其他要求。

With its bookend, error_append_string, this setting allows you to make error messages a different color than other text, or what have you.

warn_plus_overloading = Off

此设置在将 + 运算符用于字符串(比如表单值)时会发出警告。

This setting issues a warning if the + operator is used with strings, as in a form value.

variables_order = EGPCS

此配置设置取代了 gpc_order。这两个设置现在与其 register_globals 一起弃用。它设置了不同变量的顺序:环境、GET、POST、COOKIE 和 SERVER(也称为内置)。您可以更改此顺序。

This configuration setting supersedes gpc_order. Both are now deprecated along with register_globals. It sets the order of the different variables: Environment, GET, POST, COOKIE, and SERVER (aka Built-in). You can change this order around.

变量将按照从左到右的顺序逐个覆盖,最右边的每次都赢得比赛。这意味着,如果您保留默认设置并碰巧为环境变量、POST 变量和 COOKIE 变量使用了相同名称,那么在进程结束时,COOKIE 变量将拥有该名称。实际生活中,这种情况很少发生。

Variables will be overwritten successively in left-to-right order, with the rightmost one winning the hand every time. This means if you left the default setting and happened to use the same name for an environment variable, a POST variable, and a COOKIE variable, the COOKIE variable would own that name at the end of the process. In real life, this doesn’t happen much.

register_globals = Off

此设置允许您决定是否将 EGPCS 变量注册为全局变量。此设置现在已弃用,从 PHP4.2 开始,此标志默认设置为关。请改用超级全局数组。本书中的所有主要代码列表都使用超级全局数组。

This setting allows you to decide whether you wish to register EGPCS variables as global. This is now deprecated, and as of PHP4.2, this flag is set to Off by default. Use superglobal arrays instead. All the major code listings in this book use superglobal arrays.

magic_quotes_gpc = On

此设置转义传入 GET/POST/COOKIE 数据中的引号。如果您使用许多可能会提交给自己或其他表单并在表单值中显示的表单,您可能需要将此指令设置为开,或者准备对字符串类型数据使用 addslashes()。

This setting escapes quotes in incoming GET/POST/COOKIE data. If you use a lot of forms which possibly submit to themselves or other forms and display form values, you may need to set this directive to On or prepare to use addslashes() on string-type data.

magic_quotes_runtime = Off

此设置转义传入数据库和文本字符串中的引号。记住,SQL 在存储字符串时会对单引号和撇号添加斜杠,在返回时不会去除。如果此设置关闭,您需要在从 SQL 数据库输出任何类型的字符串数据时使用 stripslashes()。如果将 magic_quotes_sybase 设置为开,则此设置必须关闭。

This setting escapes quotes in incoming database and text strings. Remember that SQL adds slashes to single quotes and apostrophes when storing strings and does not strip them off when returning them. If this setting is Off, you will need to use stripslashes() when outputting any type of string data from a SQL database. If magic_quotes_sybase is set to On, this must be Off.

magic_quotes_sybase = Off

此设置不对传入数据库和文本字符串中的单引号使用反斜杠,而是使用 Sybase 样式的单引号。如果将 magic_quotes_runtime 设置为开,则此设置必须关闭。

This setting escapes single quotes in incoming database and text strings with Sybase-style single quotes rather than backslashes. If magic_quotes_runtime is set to On, this must be Off.

auto-prepend-file = [path/to/file]

如果此处指定了路径,PHP 必须在每个 PHP 文件的开头自动对其 include()。包含路径限制适用。

If a path is specified here, PHP must automatically include() it at the beginning of every PHP file. Include path restrictions do apply.

auto-append-file = [path/to/file]

如果此处指定了路径,PHP 必须在每个 PHP 文件的末尾对其 include(),除非您使用 exit() 函数转义。包括路径限制适用。

If a path is specified here, PHP must automatically include() it at the end of every PHP file.unless you escape by using the exit() function. Include path restrictions do apply.

include_path = [DIR]

如果您设置此值,您将只能从这些目录包含或需要文件。包含目录通常位于您的文档根目录下;如果您在安全模式下运行,则这是强制性的。将其设置为。,以便包含与您的脚本所在的相同目录中的文件。多个目录使用冒号分隔:.:/usr/local/apache/htdocs:/usr/local/lib。

If you set this value, you will only be allowed to include or require files from these directories. The include directory is generally under your document root; this is mandatory if you.re running in safe mode. Set this to . in order to include files from the same directory your script is in. Multiple directories are separated by colons: .:/usr/local/apache/htdocs:/usr/local/lib.

doc_root = [DIR]

如果您使用 Apache,您已经在 httpd.conf 中为该服务器或虚拟主机设置了文档根目录。如果您在使用安全模式或希望仅启用您网站的一部分的 PHP(例如,仅在 Web 根目录的一个子目录中),请在此处设置此值。

If you are using Apache, you have already set a document root for this server or virtual host in httpd.conf. Set this value here if you.re using safe mode or if you want to enable PHP only on a portion of your site (for example, only in one subdirectory of your Web root).

file_uploads = [on/off]

如果您要使用 PHP 脚本上传文件,请打开此标志。

Turn on this flag if you will upload files using PHP script.

upload_tmp_dir = [DIR]

除非您了解 HTTP 上传的影响,否则不要对这一行进行注释!

Do not comment this line unless you understand the implications of HTTP uploads!

session.save-handler = files

除了极少数情况下,您一般不会希望更改此设置。因此请不要动它。

Except in rare circumstances, you will not want to change this setting. So don’t touch it.

ignore_user_abort = [On/Off]

如果网站访问者单击浏览器的停止按钮,此设置将控制会发生的事情。默认设置为“开”,这意味着该脚本将继续运行到完成或超时。如果该设置被更改为“关”,该脚本将中止。此设置只在模块模式下工作,而不在 CGI 中工作。

This setting controls what happens if a site visitor clicks the browser’s Stop button. The default is On, which means that the script continues to run to completion or timeout. If the setting is changed to Off, the script will abort. This setting only works in module mode, not CGI.

mysql.default_host = hostname

如果未指定其他主机,则在连接到数据库服务器时要使用的默认服务器主机。

The default server host to use when connecting to the database server if no other host is specified.

mysql.default_user = username

如果未指定其他名称,则在连接到数据库服务器时要使用的默认用户名。

The default user name to use when connecting to the database server if no other name is specified.

mysql.default_password = password

如果未指定其他密码,则在连接到数据库服务器时要使用的默认密码。

The default password to use when connecting to the database server if no other password is specified.