PHP.INI之旅

tech2023-11-30  31

Anyone who has a server using PHP has undoubtedly heard of php.ini – it’s the configuration file used to control and customize PHP’s run-time behavior. It provides a simple way to configure settings for such things as:

拥有使用PHP的服务器的任何人无疑都听说过php.ini –它是用于控制和自定义PHP的运行时行为的配置文件。 它提供了一种简单的方法来配置以下各项的设置:

Upload Directories

上传目录 Log Errors

记录错误 Maximum Script execution time

最长脚本执行时间 File Upload limit

文件上传限制

… and so much more.

……等等。

php.ini is the first file PHP looks for when starting up because of the importance of the configuration directives it sets. However if you make changes to your php.ini, it will requires a server reboot before the changes take effect.

php.ini是PHP启动时会查找的第一个文件,因为它设置的配置指令非常重要。 但是,如果您对php.ini进行了更改,则需要重新启动服务器才能使更改生效。

A pre-made php.ini file with recommended settings ships with PHP. Many hosting providers who support PHP allow you some way of customizing php.ini directives so you can tweak PHP’s behavior just the way you like it. In these instances, PHP only uses the values for the directives which you have specified in your custom php.ini file; any settings you don’t declare in your file are assigned their respective values from the original php.ini and PHP’s built-in defaults.

带有建议设置的预制php.ini文件随PHP一起提供。 许多支持PHP的托管服务提供商都提供了一些自定义php.ini指令的方法,因此您可以按照自己喜欢的方式来调整PHP的行为。 在这些情况下,PHP仅使用在自定义php.ini文件中指定的指令的值。 您未在文件中声明的所有设置都会从原始php.ini和PHP的内置默认值中分配各自的值。

In this article I’ll give an overview of some important settings I believe you should be concerned with when tweaking your own php.ini file. This is just a personal selection of course, and everyone will develop their own preferences over time, so I recommend you do some solid research into the myriad of configuration directives available in php.ini.

在本文中,我将概述一些重要的设置,相信您在调整自己的php.ini文件时应该关注这些设置。 当然,这只是个人选择,随着时间的推移,每个人都会发展自己的偏好,因此我建议您对php.ini提供的无数种配置指令做一些扎实的研究。

The location of php.ini depends on the server and how PHP was installed. For the purposes of this article I’ll assume it’s at /usr/local/lib/php/php.ini. Your path may be different. To find where your php.ini file is located, you can use the phpinfo() function.

php.ini的位置取决于服务器以及PHP的安装方式。 出于本文的目的,我假设它位于/usr/local/lib/php/php.ini 。 您的路径可能不同。 要查找您的php.ini文件所在的位置,可以使用phpinfo()函数。

So let’s begin our tour of the php.ini file, the first stop: the engine directive.

因此,让我们开始游览php.ini文件,这是第一站: engine指令。

PHP引擎 (PHP Engine)

engine = On

The first configuration setting is the engine directive, which basically controls whether the PHP engine is turned on or off. This directive is responsible for determining whether PHP is available to your scripts, and setting it to Off will prevent you from using PHP at all.

第一个配置设置是engine指令,它基本控制PHP引擎是打开还是关闭。 该指令负责确定PHP是否可用于您的脚本,将其设置为Off将完全使您无法使用PHP。

You may be wondering why I have included this, since it’s obvious you should leave this enabled if you plan on using PHP. The simple answer is having this in a custom php.ini file allows you have take full control of the PHP server within your custom configuration. You will be setting all your custom rules for PHP in the file, and I believe it’s inefficient to have the master switch hidden in another file rather than present in the file in which you will be visiting most often.

您可能想知道为什么要包含此功能,因为很明显,如果您打算使用PHP,则应启用此功能。 简单的答案是在自定义php.ini文件中包含此文件,您可以在自定义配置中完全控制PHP服务器。 您将在文件中为PHP设置所有自定义规则,并且我相信将主开关隐藏在另一个文件中而不是出现在您最常访问的文件中是没有效率的。

The next stop is to a directive which I regard as being the most important when it comes to writing portable code: short_open_tag.

下一站是一个指令,在编写可移植代码时,我认为这是最重要的指令: short_open_tag 。

短标签 (Short Tags)

short_open_tag = On

The short_open_tag directive allows developers to use short tags, which in a nutshell are when a developer would write <? instead of <?php. An example using short tags is:

short_open_tag指令允许开发人员使用短标签,简而言之就是开发人员何时编写<? 而不是<?php 。 使用短标签的示例是:

<? echo "Hello World"; ?>

compared to using full tags:

与使用完整标签相比:

<?php echo "Hello World"; ?>

With short_open_tag enabled, you can use both <? and <?php. However, there a very big downside to using short-tags, especially when your are developing anything that is intended to be used on various PHP servers. Short tags are enabled by default but can cause some parsing problems so some people turn the option off. If your code depends on short tags then it would not run on their server. It is highly recommended that you turn off short-tags in your php.ini when developing portable code to prevent yourself from using them.

启用short_open_tag ,您可以同时使用<? 和<?php 。 但是,使用短标记有很大的缺点,尤其是当您正在开发打算在各种PHP服务器上使用的任何东西时。 短标签默认情况下处于启用状态,但会导致某些解析问题,因此有人关闭了该选项。 如果您的代码依赖短标签,则它将无法在其服务器上运行。 强烈建议您在开发可移植代码时关闭php.ini短标记,以防止自己使用它们。

Next, I’ll highlight an option which everyone, at any level of development ability, will have dealt with without even knowing it: output buffering.

接下来,我将重点介绍一个选项,每个开发能力水平的每个人都将在不知道的情况下进行处理:输出缓冲。

输出缓冲 (Output Buffering)

output_buffering = Off

You have probably seen a derivative of the following message on a few occasions: Cannot add header information headers already sent. This warning comes about when a scripts attempts to set an HTTP header (an element not seen by the user but processed by the web browser none the less) after it has already started sending output back to the user’s browser. Enabling output_buffering causes PHP to delay sending the headers, and instead send them and the output from your script at the same time once the script has finished processing. This allows you to change header values at any point during your script’s execution.

您可能在某些情况下看到以下消息的派生词:无法添加已经发送的头信息头。 当脚本开始将输出发送回用户浏览器后,脚本尝试设置HTTP标头(用户看不到但仍由Web浏览器处理的元素)时,就会出现此警告。 启用output_buffering会导致PHP延迟发送标头,而是在脚本完成处理后同时发送标头和脚本的输出。 这使您可以在脚本执行期间随时更改标头值。

It’s generally considered that output_buffering is a directive that can be left turned on without causing any major complications, so it can be useful to leave it turned on in case its functionality is ever needed. Personally, I prefer to leave it off because if you develop a WordPress plugin for example that will be run on many different servers, you’ll need to watch that your code don’t rely on automatic output buffering to be turned on.

通常认为output_buffering是可以保持打开状态而不会引起任何重大复杂性的指令,因此在需要其功能时将其保持打开状态很有用。 就个人而言,我宁愿将其保留下来,因为例如,如果您开发将在许多不同的服务器上运行的WordPress插件,则需要注意您的代码不依赖于自动输出缓冲来打开。

Moving on, let’s have a look at two options which can be of great benefit if you’re creating a website which the visual templates share common header and footer files: auto_prepend_file and auto_append_file.

继续,让我们看一下两个选项,如果您要创建一个可视模板共享公共页眉和页脚文件的网站,那么这两个选项将非常auto_prepend_file : auto_prepend_file和auto_append_file 。

自动页眉和页脚 (Automatic Headers and Footers)

auto_prepend_file = "header.php" auto_append_file = "footer.php"

The auto_prepend_file and auto_append_file directives let you to tell PHP to append a file to your script’s output and append a file at the end. This is reminiscent of the get_header() and get_footer() functions in WordPress, and can be very useful if you are using common header and footer files. Be aware however that the files specified with these directives will used for every PHP file, which may not be be exactly what you want.

使用auto_prepend_file和auto_append_file指令,您可以告诉PHP将文件附加到脚本输出中,并在文件末尾附加文件。 这使人想起了WordPress中的get_header()和get_footer()函数,如果您使用的是常见的页眉和页脚文件,则可能会非常有用。 但是请注意,用这些指令指定的文件将用于每个PHP文件,而这可能并不是您想要的。

An alternative use for auto_prepend_file and auto_append_file is to set up a simple execution timer which can be used to benchmark the time it takes to generate a page. For example, the file loaded by auto_prepend_file might contain:

auto_prepend_file和auto_append_file的另一种用法是设置一个简单的执行计时器,该计时器可用于基准化生成页面所需的时间。 例如,由auto_prepend_file加载的文件可能包含:

<?php $t1 = microtime(true);

… and the file for auto_append_file would contain:

…,而auto_append_file的文件将包含:

<?php $t2 = microtime(true); $elapsed = $t2 - $t1; $logFile = "/tmp/timing.txt"; file_put_contents($logFile, $elapsed . " " . $_SERVER["REQUEST_URI"] . "n", FILE_APPEND);

The next stop on our tour is PHP’s error handling directives.

我们的游览的下一站是PHP的错误处理指令。

处理错误 (Handling Errors)

error_reporting = E_ALL|E_STRICT display_errors = Off log_errors = On error_log = "/var/log/php_errors.log"

The settings I’ve shown above is the recommended configuration for PHP’s error reporting behavior in a production environment. An error in your script can either be non-fatal (such as a notice or warning), or fatal which stops your script from executing. Regardless of whether the error is fatal or non-fatal, this configuration will instruct PHP not to display the error message in its output and instead save it to an error log file.

上面显示的设置是生产环境中PHP错误报告行为的推荐配置。 脚本中的错误可以是非致命错误(例如通知或警告),也可以是致命错误,导致脚本无法执行。 无论错误是致命的还是非致命的,此配置都将指示PHP不要在其输出中显示错误消息,而是将其保存到错误日志文件中。

Turning on display_errors will output details of the error to your browser, which is handy for development purposes. But once your site is deployed, error messages could prove distracting to your users and even provide malicious users with insight on how to attack your site. It’s better to send error messages to a log file to which only you and other administrators will have access to. This can be done by setting the error_log directive. The error messages that would normally be displayed in the browser will then be logged to the specified file instead.

打开display_errors会将错误的详细信息输出到您的浏览器,这对于开发很方便。 但是,一旦部署了站点,错误消息可能会分散您的用户的注意力,甚至为恶意用户提供有关如何攻击您的站点的见解。 最好将错误消息发送到仅您和其他管理员可以访问的日志文件。 这可以通过设置error_log指令来完成。 通常将在浏览器中显示的错误消息将记录到指定文件中。

For more information on error handling, check out Sneha Heda’s article, Error Handling in PHP.

有关错误处理的更多信息,请查看Sneha Heda的文章PHP中的错误处理 。

The final stop on our tour is to have a quick look at time zones in PHP.

我们之旅的最后一站是快速浏览PHP的时区。

时区 (Time Zones)

date.timezone = "US/Central"

This setting is not set by default in php.ini and when E_STRICT reporting is enabled, PHP will issue warnings any time you use a date or time function in your script. This can be easily resolved by setting the date.timezone directive in your configuration file.

在php.ini ,默认情况下未设置此设置,并且在启用E_STRICT报告后,只要您在脚本中使用日期或时间函数,PHP都会发出警告。 通过在配置文件中设置date.timezone指令,可以轻松解决此问题。

This option is often over looked by developers, but it is one that if configured correctly, can reduce the chance of a huge amount of complications accruing in your PHP script somewhere down the road. A full list of supported time zones is available in the online documentation.

开发人员通常会忽略此选项,但如果配置正确,则可以减少PHP脚本中大量麻烦的可能性。 在线文档中提供了所支持的时区的完整列表。

摘要 (Summary)

It is highly recommenced that all web developers take a look through their server’s php.ini file and familiarize themselves with its contents, and personalize some of the directives as appropriate. The configuration directives in this article should give you a good place to start. If you’re using a shared host, the configuration set up by the hosting company may not always be the best and may not compliment your coding style. Check with your provider to learn what options are available to you to customize your environment.

强烈建议所有Web开发人员浏览其服务器的php.ini文件,并熟悉其内容,并根据需要对某些指令进行个性化设置。 本文中的配置指令应为您提供一个良好的起点。 如果您使用的是共享主机,则托管公司设置的配置可能并不总是最佳的,并且可能不符合您的编码风格。 请与您的提供商联系,以了解可以使用哪些选项来自定义环境。

Image via Laborant / Shutterstock

图片来自Laborant / Shutterstock

翻译自: https://www.sitepoint.com/a-tour-of-php-ini/

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)