


Detailed explanations and solutions to common security issues in PHP development
PHP开发中常见的安全问题详解和解决方法(如Sql注入、CSRF、Xss、CC等)
这篇文章主要介绍了PHP开发中常见的安全问题详解和解决方法,详细介绍了例如Sql注入、CSRF、Xss、CC等攻击手段的背景知识以及解决方法,需要的朋友可以参考下
浅谈Php安全和防Sql注入,防止Xss攻击,防盗链,防CSRF
前言:
首先,笔者不是web安全的专家,所以这不是web安全方面专家级文章,而是学习笔记、细心总结文章,里面有些是我们phper不易发现或者说不重视的东西。所以笔者写下来方便以后查阅。在大公司肯定有专门的web安全测试员,安全方面不是phper考虑的范围。但是作为一个phper对于安全知识是:“知道有这么一回事,编程时自然有所注意”。
1、php一些安全配置
(1)关闭php提示错误功能
在php.ini 中把display_errors改成
<code><span>display_errors = <span><span>OFF</span></span></span></code>
或在php文件前加入
<code><span><span>error_reporting</span><span>(<span>0</span>)</span></span></code>
1)使用error_reporting(0);失败的例子:
A文件代码:
<code><span><span></span>error_reporting(<span>0</span>); <span>echo</span><span>555</span><span>echo</span><span>444</span>; <span>?></span></span></code>
错误:
<code>Parse error: parse error, expecting <span>`','</span>' or <span>`';'</span>' in E:\webphp\2.php on line 4</code>
2)使用error_reporting(0);成功的例子:
a文件代码:
<code><span><span><?php </span>error_reporting(<span>0</span>); <span>include</span>(<span>"b.php"</span>); <span>?></span></span></span></code>
b文件代码:
<code><span><span><?php </span><span>echo</span><span>555</span><span>echo</span><span>444</span>; <span>?></span></span></span></code>
这是很多phper说用error_reporting(0)不起作用。第一个例子A.php里面有致命错误,导致不能执行,不能执行服务器则不知有这个功能,所以一样报错。
第二个例子中a.php成功执行,那么服务器知道有抑制错误功能,所以就算b.php有错误也抑制了。
ps:抑制不了mysql错误。
(2)关闭一些“坏功能”
1)关闭magic quotes功能
在php.ini 把magic_quotes_gpc = OFF
避免和addslashes等重复转义
2)关闭register_globals = Off
在php.ini 把register_globals = OFF
在register_globals = ON的情况下
地址栏目:http://www.jb51.net?bloger=benwin
<code><?php //<span>$bloger = <span>$_GET</span>[<span>'bloger'</span>] //因为register_globals = ON 所以这步不用了直接可以用<span>$bloger</span><span>echo</span><span>$bloger</span>; ?></code>
这种情况下会导致一些未初始化的变量很容易被修改,这也许是致命的。所以把register_globals = OFF关掉
(3)严格配置文件权限。
为相应文件夹分配权限,比如包含上传图片的文件不能有执行权限,只能读取
2、严格的数据验证,你的用户不全是“好”人。
记得笔者和一个朋友在讨论数据验证的时候,他说了一句话:你不要把你用户个个都想得那么坏!但笔者想说的这个问题不该出现在我们开发情景中,我们要做的是严格验证控制数据流,哪怕10000万用户中有一个是坏用户也足以致命,再说好的用户也有时在数据input框无意输入中文的时,他已经不经意变“坏”了。
2.1为了确保程序的安全性,健壮性,数据验证应该包括
(1) 关键数据是否存在。如删除数据id是否存在
(2) 数据类型是否正确。如删除数据id是否是整数
(3) 数据长度。如字段是char(10)类型则要strlen判断数据长度
(4) 数据是否有危险字符
数据验证有些人主张是把功能完成后再慢慢去写安全验证,也有些是边开发边写验证。笔者偏向后者,这两种笔者都试过,然后发现后者写的验证相对健壮些,主要原因是刚开发时想到的安全问题比较齐全,等开发完功能再写时有两个问题,一个phper急于完成指标草草完事,二是确实漏掉某些point。
2.2程序员容易漏掉point或者说需要注意的事项:
(1) 进库数据一定要安全验证,笔者在广州某家公司参与一个公司内部系统开发的时候,见过直接把
以上就介绍了PHP开发中常见的安全问题详解和解决方法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version