php万能查询用预,PHP 与 mysql

一、php 的 sql 注入攻击

1.1、什么是 sql 注入攻击

用户提交一段数据库查询代码,根据返回的结果,获得某些他想得到的数据。

比如 :查询某个管理员是否存在,一般程序员会这么写$sql = "select * from user where name='luluyii' and password='****'";

if (查询到) {

header("admin.php");

}else {

header("login.php");

}

这样写,容易被人通过 sql 注入攻击进入你的 admin.php 页面。

1.2、万能密码和万能用户名

(1)方式一

① 万能密码$sql = "select * from user where name='**' and password='***' or 1='1'";

即万能密码是:**' or 1='1

② 万能用户名$sql = "select * from user where name='**' union select * form user/* and password='***';

/*表示不执行后面的语句,即万能用户名是:**' union select * from user/*

(二)方式二$sql = 去掉 '$name' 和 '$password'的单引号;

这种写法,没有' ',mysql数据库会把你的输入当数字对待

① 使用万能密码:数字 union select * form user

② 使用万能用户名:数字 union select * form user/*

1.3、防止用户登录注入

(1)在php.ini 中开启 magic_quotes_gpc = On,但是这个特性在 php 5.3.0 中已经废弃,并且在 php 5.4.0 中已经移除。

若配置了,方式一中的万能用户名和万能密码将失效,但是方式二仍然有效。

原因是:当改为 on 之后,Apache 服务器会对 ' 加入 \ 转义,如name = 'luluyii',当数据库执行时,变成 name = \'luluyii\'

(2)密码对比法

① 基本思想:改变验证数据库用户逻辑。

首先通过用户输入的用户名去查询数据库,如果查询到这个用户对应的密码,和用户提交的密码对比,相同则说明该用户合法,否则不合法。$sql = "select password form user where name='luluyii'";

if(从数据库查询到的密码 == 用户输入的密码){

header("admin.php");

}else{

header("login.php");

}

分析:这样写,用户无法通过 password = 注入攻击,因为 if( == ) 是在php中验证的。

② 使用 pdo 的预编译来解决 sql 注入

首先在 php,ini 中启用 pdo:extension = php_pdo_mysql.dll$mypdo = new PDO("mysql:localhost;port=3306;dbname=luluyii","root","root");

$pdoStatment = $mypdo->prepare($sql); //预编译

$pdoStatment->execute(array($name,$password)); //接收

$pdoStatment->fetch(); //取出结果

1.4、php 搜索引擎中 sql 注入问题

(1)一般使用 _ _ 和 % 攻击,获取所有数据

(2)防止查询 sql 攻击$keyWord = addslashes($keyWord); //使用反斜杠引用字符串

$keyWord = str_replace("%","\%",$keyWord); //将 % 替换为 \%

$keyWord = str_replace("_","\_",$keyWord); //将 _ 替换为 \_

1.5、总结

防止 SQL 注射漏洞一般用什么函数?addslashes() 函数返回在预定义字符之前添加反斜杠的字符串。

$str = addslashes('Shanghai is the "biggest" city in China.');

echo($str);

//输出 Shanghai is the \"biggest\" city in China.

sql 注入的方式还有其它很多形式,我们要写出健壮安全的代码,就要不断提高编写安全代码的意识,让我们的代码更符合

商业要求。

二、php 数据库编程 mysqli、pdo

2.1、mysqli

2.1.1、mysql 和 mysqli 说明

(1)msyqli 是 mysql 扩展库的增强版

(2)mysql 和 mysqli的比较

① mysqli 的稳定性、安全性、效率都有所提升

② mysqli 支持面向对象编程,同时考虑到 php 老程序员,提供面向过程的编程风格

2.1.2、案例$conn = mysqli_connect("localhost",'root','root');

if (!$conn){

die("数据库连接失败".mysqli_error());

}

mysqli_select_db($conn,"luluyii");

mysqli_query($conn,"set names utf8");

$sql = "select * from lulu_user";

$res = mysqli_query($conn,$sql);

while($row = mysqli_fetch_row($res)){

foreach ($row as $key=>$val){

echo "$val--";

}

echo "
";

}

mysqli_free_result($res);

mysqli_close($conn);

2.1.3、细节说明

(1)关闭连接和释放资源

76627b17686334763e3d2a8be5624bef.gifmysqli_close($conn); //关闭连接,是把 ① 断开;

mysqli_free_result($res); //释放资源,是把 ② 断开。

(2)从 $res 获取行数据的四个方法① mysqli_fetch_row 返回一个索引数组

② mysqli_fetch_assoc 返回一个关联数组

③ mysqli_fetch_array 返回索引和关联数组

④ mysqli_fetch_object 把一行数据当做对象返回

(3)函数mysqli_insert_id($conn)  取得上一步 Insert 操纵产生的 ID

$field_info = mysqli_fetch_field($res) 返回包含字段信息的对象,如$field_info->name

mysqli_affected_rows($conn) 取得前一次mysql操作所影响的记录行数

(4)三种释放 $res 结果集的方法

① $res->free(); ② $res->close(); ③ $res->free_result();

2.1.4、批量执行sql 语句

ddl 数据定义语句:CREATE TABLE

dml 数据操作语句:update、insert、delete

dql 数据查询语句:select

dcl 数据事务语句:rollback、commit

(1)批量执行 dml 语句$sqls = "sql1;sql2;...";

$mysqli->multi_query($sqls);

(2)批量执行 dql 语句$mysqli->store_result(); //从 mysqli 连接取出一个结果集

$mysqli->fetch_row();

$mysqli->more_results(); //用于判断是否有新的结果集

$mysqli->next_result(); //用于指向下一个结果集,但它不会判断下一个结果集是否存在,故需使用 $mysqli->more_results()

2.2、事务控制

2.2.1、概念

事务用于保证数据的一致性,它由一组相关的 dml 语句组成,该组的 dml 语句要么全部成功,要么全部失败。

如:网上转账就是典型的要用事务来处理,用以保证数据的一致性。

2.2.2、事务的 ACID 性质

① Atomicity 原子性:操作不可分割,要么都发生,要么都不发生

② Consistency 一致性:是一个数据库从一个一致性状态到另一个一致性状态

③ Isolation 隔离性:一旦开始事务,别的操作无法使用你正在使用的数据库

④ Durability 持久性:一旦提交,数据库的状态不再发生变化

2.2.3、在 mysqli 控制台可以使用事务来操作

① start transaction 开启一个事务

② savepoint a 保存点

③ 操作

④ 可回滚,可提交:若无问题 —— commit 提交,若有问题 —— rollback to a 回滚到 a

2.3、mysqli 扩展库的预编译处理技术

76627b17686334763e3d2a8be5624bef.gif$sql = "insert into user (name,password,email,age) values(?,?,?,?)";

这里的 ? 是个占位符,告诉数据库无需编译,只是数据的变化。

优点:效率高,执行速度快;安全性高,防止 sql 注入。

2.4、PDO —— PHP Data Object

2.4.1、基本介绍

① 该扩展在 PHP 5 中加入

② PHP 6 中默认使用 PDO 连接数据库

2.4.2、什么是 PDO

PDO 相当于是一个数据库抽象层,不同数据库使用相同的方法名,解决数据库连接不统一的问题。

76627b17686334763e3d2a8be5624bef.gif

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441672.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php 判断radio选中哪个,jquery如何判断单选按钮radio是否选中

jquery判断单选按钮radio是否选中的方法:1、加载页面的时候获取id,代码为【var fs$("#"id).val()】;2、点击按钮的时候获取id,代码为【var id $(this).attr("id")】。本教程操作环境:windows7系统…

【qduoj】【超级楼梯进阶版】

题干: 描述 N级阶梯,人可以一步走一级,也可以一步走两级,求人从阶梯底端走到顶端可以有多少种不同的走法。 输入 一个整数n,代表台阶的阶数。 输出 求人从阶梯底端走到顶端可以有多少种不同的走法,输出结…

matlab在光学实验中的应用,matlab在光学实验中的应用

matlab在光学实验中的应用 《MATLAB》课程论文MATLAB 在光学实验中的应用姓名:学号:专业:班级:指导老师:学院:完成日期:1MATLAB 在波动光学中的应用(姓名:郑苗苗 12012241736 2012 级…

【HDU - 6016】Count the Sheep (思维,类似二分图)

题干: Altough Skipping the class is happy, the new term still can drive luras anxious which is of course because of the tests! Luras became worried as she wanted to skip the class, as well as to attend the BestCoder and also to prepare for test…

如何生成时间序列matlab,求助:在MATLAB里如何输入时间序列中的时间

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼function[logRS,logERS,V]RSana(x,n,method,q)%Syntax:[logRS,logERS,V]RSana(x,n,method,q)%____________________________________________%% Performs R/Sanalysis on a time series.%% logRS is thelog(R/S).% logERS is theEx…

【CF#192 A】Funky Numbers (二分,查找,二元组)

题干: As you very well know, this years funkiest numbers are so called triangular numbers (that is, integers that are representable as , where k is some positive integer), and the coolest numbers are those that are representable as a sum of two…

matlab考试试题,matlab-考试试题-

matlab-考试试题- MATLAB 考试试题 (1) 产生一个1x10的随机矩阵,大小位于( -5 5),并且按照从大到小的顺序排列好!(注:要程序和运行结果的截屏)答案:a10*rand(1,10)-5;bsort(a, descend )1.请产生一个100*5 的矩阵&…

【HDU - 1702 】ACboy needs your help again! (栈和队列,水题模拟)

题干: ACboy was kidnapped!! he miss his mother very much and is very scare now.You cant image how dark the room he was put into is, so poor :(. As a smart ACMer, you want to get ACboy out of the monsters labyrinth.But when you arrive at the g…

java中jframe不存在怎么办,java – 设置JFrame背景,为什么这不起作用?

打印加载图像的宽度(如果为-1)则图像未正确加载.img Toolkit.getDefaultToolkit().createImage("red.png");System.out.println(img.getWidth(null)); // check what it prints值得阅读Loading Images Using getResource上的Java Tutorial您可以根据图像位置尝试任何…

后盾网经典原创视频教程php,《后盾网经典原创视频教程:PHP》139集

目录0_1 后盾网_IIS环境下PHP开发环境安装0 后盾网_PHP集成环境安装视频教程1 PHP视频教程 PHP基础(一)2 PHP视频教程 PHP基础(二)3 PHP视频教程 PHP基础(三)4 PHP视频教程 数据类型(一)5 PHP视频教程 数据类型(二)6 PHP视频教程 数据类型(三)7 PHP视频教程 类型转换 外部变量8…

【HDU - 1022】Train Problem I (栈模拟,水题,思维)

题干: As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a proble…

【HDU - 1216 】Assistance Required (模拟,类似素数打表,不是素数问题!)

题干: After the 1997/1998 Southwestern European Regional Contest (which was held in Ulm) a large contest party took place. The organization team invented a special mode of choosing those participants that were to assist with washing the dirty d…

任意阶魔方阵matlab程序,【精品】任意阶魔方阵算法(c语言)

n阶幻方是由前n^2(n的2次方)个自然数组成的一个n阶方阵,其各行、各列及两条对角线所含的n个数的和相等。洛书就是最基本的33阶魔方阵,做出某种最恰当的决定,横竖都有3个格。 0的倒数 a-1可以对于 n 阶单位矩阵 e 以及同阶的方阵 a…

【HDU - 1302】The Snail (模拟,水题)

题干: A snail is at the bottom of a 6-foot well and wants to climb to the top. The snail can climb 3 feet while the sun is up, but slides down 1 foot at night while sleeping. The snail has a fatigue factor of 10%, which means that on each succe…

悟空php微信复制的东西在哪找,微信收藏的文件在哪?从哪里能看到?

现在的微信有很多的小功能,非常的方便实用,但是很多功能大家都不知道,今天,开淘网小编就来教教大家怎么使用微信的“我的收藏”功能。这个功能非常实用,而且收藏的源文件删除的话,我们从收藏里还是一样能用…

python批量打印机excel,python自动化办公系列03_单个以及批量处理excel文件

先贴上数据集,链接:https://pan.baidu.com/s/1ttv7NwbRmfVPcj2iBHTAfg提取码:zg5v下面是关于如何计算每个销售额以及总销售的代码。import osimport pandas as pdos.chdir("C:\\Users\\yuyuk\\data science\\data analysis and descript…

【OpenJ_Bailian - 2299 】Ultra-QuickSort (归并排序 或 离散化 + 树状数组)

题干: In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequ…

redis阻塞队列 java,Redis阻塞/非阻塞队列

非阻塞队列RPUSH key value [value ...]RPOP keyLPUSH key value [value ...]LPOP keyR/LPUSH都是后进先出操作,组合起来则是先进先出,属于非阻塞操作,即:不管有无找到指定键值都立即返回。阻塞队列RPUSH key value [value ...]BR…

*【CodeForces - 791B】Bear and Friendship Condition (图论,判断完全图,dfs乱搞或带权并查集)

题干: Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends.…

matlab aviobj,MATLAB AVI 视频读取处理

MATLAB AVI 视频读取处理1、用matlab读取avi视频(只能读一定压缩各式的avi 电影,这是因为avi视频文件的编码有很多,而matlab只支持部分编码格式。可见http://www.doczj.com/doc/4ae6c6222af90242a895e5fd.html/IdoIwill/article/details/2125838)aviinfo…