php防注入及开发安全详细解析
1、PHP注入的基本原理
程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对 用户输入数据的合法性进行判断,使应用程序存在安全隐患。用户可以提交一段数据 库查询代码,根据程序返回的结果,获得某些他想得知的数据,这就是所谓的 SQL Injection,即SQL注入。 受影响的系统:对输入的参数不进行检查和过滤的系统.
SQL注入过程
正常来讲,我们通过地址接收一些必要的参数如:
页面中我们会使用 2 写入到SQL语句中
正常情况:Select * From Table where id=2
bkJia.php?id=2
如果我们对SQL语句熟悉,就知道2 我们可以替换成我们需要的SQL语句
如:and exists (select id from admin)
2、防止注入的几种办法
其实原来就是我们需要过滤一些我们常见的关键字和符合如:
Select,insert,update,delete,and,*,等等
例子:
复制代码 代码如下:
function inject_check($sql_str) {
return preg_match('/select|insert|update|delete|/'|///*|/*|/././/|/.//|union|into|load_file|outfile/i', $sql_str); // 进行过滤
}
或者是通过系统函数间的过滤特殊符号
Addslashes(需要被过滤的内容)
3、PHP其他地方安全设置
register_globals = Off 设置为关闭状态
SQL语句书写时尽量不要省略小引号和单引号
复制代码 代码如下:
Select * From Table Where id=2 (不规范)
Select * From ·Table· Where ·id·='2' (规范)
提高数据库命名技巧,对于一些重要的字段可根据程序特点命名
对于常用方法加以封装,避免直接暴露SQL语句
正确的使用 $_POST $_GET $_SESSION 等接受参数,并加以过滤

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
