PHP是非常强大的服务器端脚本语言,但是强大的功能总是伴随着重大的危险,PHP本身老版本有一些问题,比如在 php4.3.10和php5.0.3以前有一些比较严重的bug,所以推荐使用新版。另外,目前闹的轰轰烈烈的SQL Injection也是在PHP上有很多利用方式,所以要保证安全,PHP代码编写是一方面,PHP的配置更是非常关键。在这节里,你将学习修改 PHP.ini文件来阻止一些PHP潜在的危险因素 。
一、 php.ini文件的选择
设定有效的 PHP 配置文件,php.ini。压缩包中包括两个 ini 文件,php.ini-dist 和 php.ini-recommended。建议使用 php.ini-recommended,因为此文件对默认设置作了性能和安全上的优化。仔细阅读此文件中的说明并研究 ini 设置 一章来亲自人工设定每个项目。如果要达到最佳的安全效果,则最好用这个文件,尽管 PHP 在默认的 ini 文件下也工作的很好。将选择的 ini 文件拷贝到 PHP 能够找到的目录下并改名为 php.ini。PHP 默认在 Windows 目录下搜索 php.ini:
在 Windows 9x/ME/XP 下将选择的 ini 文件拷贝到 %WINDIR%,通常为 c:\windows。
在 Windows NT/2000 下将选择的 ini 文件拷贝到 %WINDIR% 或 %SYSTEMROOT% 下,通常为 c:\winnt 或 c:\winnt40 对应于服务器版本。
如果在 Windows NT,2000 或 XP 中使用了 NTFS,确保运行 webserver 的用户名对 php.ini 有读取的权限(例如使其对 Everyone 可读)。
php.ini -dist 一般用于程序开发。
php.ini -recommended 用于线上服务使用。
二、 php.ini文件的修改
php的安全模式是个非常重要的内嵌的安全机制,能够控制一些php中的函数,比如system(),同时把很多文件操作函数进行了权限控制,也不允许对某些关键文件的文件,比如/etc/passwd,但是默认的php.ini是没有打开安全模式的,我们把它打开。
1、找到"safe_mode=off"改为"safe_mode=on"
象一些能执行系统命令的函数shell_exec()和``被禁止,其它的一些执行函数如:exec(), system(), passthru(),popen()将被限制只能执行safe_mode_exec_dir指定目录下的程序。如果你实在是要执行一些命令或程序,找到 以下:
safe_mode_exec_dir =
指定要执行的程序的路径,如:
safe_mode_exec_dir = c:\wwwroot
然后把要用的程序拷到c:\wwwroot目录下,这样,象上面的被限制的函数还能执行该目录里的程序
2、找到"display_errors=on"改为"display_errors=off"
一般php在没有连接到数据库或者其他情况下会有提示错误,一般错误信息中会包含php脚本当前的路径信息或者查询的SQL语句等信息,这类信息提供给黑客后,是不安全的,所以一般服务器建议禁止错误提示 。
display_errors = Off
log_errors = On
同时也要设置错误日志存放的目录 找到下面这行
;error_log = filename
去掉前面的;注释,把filename改为指定文件
error_log = D:/usr/php_error.log
3、找到"disable_functions="改为:"disable_functions=phpinfo,system,exec,passthru,shell_exec,popen,is_dir".
我们觉得不希望执行包括system()等在内的能够执行命令的php函数,或者能够查看php信息的phpinfo()等函数,那么我们就可以禁止它们:
4、查找:magic_quotes_gpc 如果是Off的话改成On
打开magic_quotes_gpc来防止SQL注入。
5、查找:register_globals
在PHP中提交的变量,包括使用POST或者GET提交的变量,都将自动注册为全局变量,能够直接访问,这是对服务器非常不安全的,所以我们不能让它注册为全局变量,就把注册全局变量选项关闭:
6、查找:open_basedir 后面增加 /www/ /*说明:www为网站程序所放文件*/
这个选项可以禁止指定目录之外的文件操作,还能有效地消除本地文件或者是远程文件被include()等函数的调用攻击。
7、expose_php设为off ,这样php不会在http文件头中泄露信息。
我们为了防止黑客获取服务器中php版本的信息,可以关闭该信息斜路在http头中 。
8、设置“allow_url_fopen”为“off” 这个选项可以禁止远程文件功能 。

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.


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

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

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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