Home php教程 php手册 PHP.INI配置文件漫游(2)

PHP.INI配置文件漫游(2)

Jun 21, 2016 am 09:14 AM
path php post windows

文章第一部分已经带领你领略了php.ini文件的结构,并且说明了如何修改PHP查找路径、差错处理,以及解析器的相关选项。第二部分将深入配置文件,内容包括如何激活PHP扩展选项、针对PHP脚本设置资源限制,以及通过PHP脚本动态改变配置变量。

激活扩展选项


PHP可以使用很多不同的扩展选项。在UNIX系统中,扩展选项需要在编译时创建;而对于Windows,二进制DLL文件将随PHP发布而将自己包括进去。变量extension_dir包括了PHP应当查看相关扩展选项的目录名。

extension_dir = "C:\Program Files\Internet Tools\Apache\bin\php4\extensions"

Windows下的PHP包括了20种不同的扩展选项,并且全部都列在了php.ini文件中(通过注释)。要激活某个特定的扩展选项只需要将相应行首的分号去掉并重启服务器就可以了。如果要禁用某个扩展选项(比如在需要提升系统性能的情况下),只需要在行首重新添加分号。

如果扩展选项没有列在php.ini文件中,可以使用变量extension,然后将相应DLL文件名传递给此变量。

extension=php_domxml.dll

extension=php_dbase.dll

设置extension-specific变量
变量extension-specific被存放在配置文件中一个单独的区域。比如,所有同MySQL扩展功能相关的变量都应当存储在php.ini中的[MySQL]区域。

如果需要使用PHP的mail()函数,则需要设置以下三个变量。当通过PHP mail()函数发送电子邮件信息时需要使用SMTP以及变量sendmail_from(Windows系统)或变量sendmail_path(UNIX系统)。对于Windows,这些变量设置了所用到的SMTP服务器以及显示在电子邮件信息中的“From:”地址;而对于UNIX,变量sendmail_path则对邮件传输设置了MTA(邮件传输代理,mail transfer agent)的路径。

SMTP = myserver.localnet.com

sendmail_from = me@localhost.com

sendmail_path = /usr/sbin/sendmail

变量java.class.path、java.home、java.library以及java.library.path全部用来设置查找Java类以及库的路径。这些值将被Java扩展所使用,所以如果希望PHP能正确的同Java程序进行集成,则必须确保这些变量被正确的设置。

java.class.path = .\php_java.jar

java.home = c:\jdk

java.library = c:\jdk\jre\bin\hotspot\jvm.dll

java.library.path= .\

变量session.save_path指定了保存会话信息所需的临时目录。通常而言,此目录默认为/tmp,但由于在Windows系统中此默认目录并不存在,所以你必须将其重新设置为正确的Windows临时目录,否则会话处理程序将会在调用session_start()函数时弹出讨厌的出错信息。同时可以通过变量session.cookie_lifetime来控制会话cookie的有效期。

session.save_path = c:\windows\temp

session.cookie_lifetime = 1800
安全设置
在php.ini中,有很多同PHP安装的安全问题相关的变量。其中最有趣的是safe_mode变量,建议针对ISP以及共享主机服务(shared-hosting services)对其进行设置,此变量将对用户对PHP的使用范围进行限制。



safe_mode = Off

当safe mode打开的时候,可以通过变量safe_mode_include_dir指定在何目录中查找相关文件。通过将二进制程序放在特定目录并使用safe_mode_include_dir变量将目录告之PHP,PHP将对可使用exec()命令运行PHP脚本的程序种类进行限制。在此目录中仅二进制文件可以通过exec()命令进行访问。

safe_mode_include_dir = /usr/local/lib/php/safe-include

safe_mode_exec_dir = /usr/local/lib/php/safe-bin

还可以通过变量open_basedir来限制文件操作。此变量将设置作为文件操作根(root)的目录名。此变量设置后,对于PHP来说,存放在此目录树以外的文件将无法访问。这对于在共享系统中将用户限制在各自的home或者Web目录是一种很好的方法。

open_basedir = /home/web/

变量max_execution_time设置了在强制终止脚本前PHP等待脚本执行完毕的时间,此时间以秒计算。当脚本进入了一个无限循环状态时此变量非常有用。然而,当存在一个需要很长时间完成的合法活动时(例如上传大型文件),这项功能也会导致操作失败。在这样的情况下必须考虑将此变量值增加,以避免PHP在脚本正在执行某些重要过程的时候将脚本关闭。

max_execution_time = 90

刚提到了上传,现在来看看如何配置uploads变量以及form变量。
配置文件上载以及表单变量
如果我们在文章前面所讨论安全配置所提供的安全强度还不能满足你的要求,可以通过关闭文件上传或设置每次上传最大文件尺寸限制来进一步提高安全强度。以上两种功能将分别通过变量file_uploads和upload_max_filesize来实现。通常来说,除非系统中有为接收文件而设计的应用程序(例如基于Web FTP服务的图片册),你应当设置一个相对较小的文件尺寸限制值。



file_uploads = On

upload_max_filesize = 2M

如果你对于上传文件并不关心,但在PHP应用程序中使用了大量的表单,这里有两个变量将会让你产生很大的兴趣。首先是变量register_globals,它解决了PHP开发人员长期以来的心头之痛。在PHP 3.x中,此变量默认为On。由此当提交表单时表单变量将自动的转换成PHP变量。

在PHP 4.x中,出于安全考虑,此变量默认设置为Off。由此,表单变量将只能通过特定的$_GET以及$_POST进行访问。这也造成了很多用PHP 3.x编写的脚本在运行时出现问题,要求开发人员重写脚本并对其重新测试。比如,输入到表单域中的值对于PHP 3.x脚本来说将可以理解为$email;而在PHP 4.x脚本中却被作为$_POST['email']或者$_GET['email']。

通常情况下可以将此变量设置为Off,这样可以对通过表单进行的脚本攻击提供更为安全的防范措施。如果需要考虑到对早期PHP 3.x脚本的兼容问题,则应当置于On。

register_globals = Off

同表单提交相关的一个变量是post_max_size,它将控制在采用POST方法进行一次表单提交中PHP所能够接收的最大数据量。似乎不大可能需要将默认的8 MB改得更大。相反,应当适当将其降到更为实际的数值。但如果希望使用PHP文件上传功能,则需要将此值改为比upload_max_filesize还要大。

post_max_size = 8M

在PHP 5中增加了max_input_time变量。此变量可以以秒为单位对通过POST、GET以及PUT方式接收数据时间进行限制。如果应用程序所运行环境处在低速链路上,则需要增加此值以适应接收数据所需的更多时间。

max_input_time = 90
性能调整
你还可以通过调整一些变量值来提升PHP解析器的性能。为了避免正在运行的脚本大量使用系统可用内存,PHP允许定义内存使用限额。通过memory_limit变量来指定单个脚本程序可以使用的最大内存容量:



memory_limit = 8M

变量memory_limit的值应当适当大于post_max_size的值。

另一项可以用于提高性能的方法是禁用变量$argc以及$argv,这两个值被用于存放在命令行中传递给应用程序的参数数量以及实际参数值。

register_argc_argv = false

类似,还可以禁用$HTTP_GET_VARS以及$HTTP_POST_VARS,因为在使用$_GET以及$_POST的今天你不大可能用到前两种方式。禁用此功能可以带来性能上的提升,但这只能通过PHP 5中的变量register_long_arrays实现。

register_long_arrays = false

函数ini_set()

最后,需要注意ini_set()函数。当PHP读取php.ini配置文件中的所有设置信息的同时,它还提供了采用ini_set()函数根据per-script原则更改这些设置的功能。此函数接收两个参数:需要调整的配置变量名,以及变量的新值。例如,在某脚本出现时增加最大执行时间(maximum execution time):


ini_set('max_execution_time', 600)

// more code

?>

这样的设置将仅仅影响被设置的脚本。一旦脚本执行完毕,该变量将自动恢复到原始值。

如果PHP应用程序运行在一个共享服务器上,你不大可能可以访问到主php.ini配置文件。这时,函数ini_set()就可以允许根据特殊要求对PHP配置做动态修改,这将会给你带来很大的方便。




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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

How to speed up the loading speed of PS? How to speed up the loading speed of PS? Apr 06, 2025 pm 06:27 PM

Solving the problem of slow Photoshop startup requires a multi-pronged approach, including: upgrading hardware (memory, solid-state drive, CPU); uninstalling outdated or incompatible plug-ins; cleaning up system garbage and excessive background programs regularly; closing irrelevant programs with caution; avoiding opening a large number of files during startup.

How to distinguish between closing a browser tab and closing the entire browser using JavaScript? How to distinguish between closing a browser tab and closing the entire browser using JavaScript? Apr 04, 2025 pm 10:21 PM

How to distinguish between closing tabs and closing entire browser using JavaScript on your browser? During the daily use of the browser, users may...

See all articles