All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
在.net,App中集成COM组件的一些简单技巧_.net资料_编程技术
sql server 如何复制数据库结构_[SQL Server教程]
asp.net2.0内置Application对象的应用
修复SQL Server的MDF文件_[SQL Server教程]
.Net环境下基于Ajax的MVC方案_.net资料_编程技术
C#中动作类型控件应用实例
ASP.NET实例:在GridView使用HyperLinkField,属性的链接_[Asp.Net教程]
Visual C++ 6.0教程:c++程序的组成之关键字
PHP技巧:关于cookie和session的分析_[PHP教程]
Delphi以XPManifest组件显示界面
Reading number is top 10 pictures
Sora aoi on twitter3
Sora aoi mirror memorial classics2
Chinese paper-cut grilles art appreciation5
A man's favorite things16
男人帮杂志里的惹火性感美女1
农夫山泉变身记
泳装名模阿尔贝特妮写真
Other people's teacher VS my teacher
狗狗与主人神同步1
避免防盗门的猫眼变成钥匙眼
Download software ranking
SQL2000 For 4IN1
matrix3
jdk1.5
WebService在.NET中的实战应用教学视频 → 第4集
电车之狼R
天龙八部十二宫服务端
C#与.NET技术平台实战演练
Boxer vs Yellow2
Boxer's Top ten classic battle7
Tram sex maniac 2 (H) rar bag18
归海一刀 published in(发表于) 2014/2/17 7:24:59 Edit(编辑)
PHP与MySQL中的SQL注入式漏洞_[PHP教程]

PHP与MySQL中的SQL注入式漏洞_[PHP教程]

PHP与MySQL中的SQL注入式漏洞_[PHP教程]

SQL注入式漏洞是许多PHP程序的主要安全危害,产生的原因是在向数据库执行插入等语句时,web开发者允许最终用户操作变量(例如根据表单提交内容显示相应信息),通常是_GET、_POST或_SESSION等全局变量。

让我们看以下的代码:
query = "Select news_title, news_text ";
query .= "FROM news";
query .= "Where news_id=". _GET['id'];

mysql_query(query);
?>

如果认为其中的_GET[‘id’]会永远是个数值型的值那将是很严重的错误。最终用户可以改变这个变量的值,例如"0; Delete FROM news;",那么query语句就会变成下面的值:

Select news_title, news_text FROM news Where news_id=0; Delete FROM news;

这将产生很严重的后果。


验证数值型数据

数值型数据是最容易验证的,PHP有一个自带的函数叫 is_numeric()可以返回ture值来判断是否是数值型,这个函数并不是MySQL自带的,因此可在任何数据库平台的php程序中用于验证数字。

下面是修改后的代码:

if (!is_numeric(_GET['id']))
{
// id's not numeric?
// kill the script before the query can run
die("The id must be numeric!");
}

query = "Select news_title, news_text ";
query .= "FROM news";
query .= "Where news_id=". _GET['id'];

mysql_query(query);
?>

验证非数值型数据


非数值型数据的验证稍有点麻烦。PHP有个叫Magic Quotes的特殊功能。当它激活时,PHP会自动过滤掉_GET和_POST全局变量中的反斜线符号(\),双引号(”),单引号(’)和空白字符。问题是并不是所有的服务器都能打开了这个功能,所以必须检测服务器是否开通了这个功能。可以使用get_magic_quotes_gpc()函数来判定maigc quotes功能是否打开。
在MySQL查询语句可以使用mysql_real_escape_string()函数来增强安全性,代码如下:

// Fix a _POST variable called firstName for MySQL
firstName = _POST['firstName'];
if (get_magic_quotes_gpc())
{
// If magic quotes is enabled - turn the string back into an unsafe string
firstName = stripslashes(firstName);
}

// Now convert the unsafe string into a MySQL safe string
firstName= mysql_real_escape_string(firstName);

// firstName should now be safe to insert into a query
?>

输出到页面

为正确显示字符中的引号和反斜线,应使用stripslashes()函数

firstName = _POST['firstName'];
if (get_magic_quotes_gpc())
{
// If magic quotes is enabled - turn the string back into an unsafe string
firstName = stripslashes(firstName);
}

// Now convert the unsafe string into a MySQL safe string
firstName = mysql_real_escape_string(firstName);

// Safe query
mysql_query("Insert INTO Names VALUES('". firstName ."')");

// Page output should look proper
echo "Hello ". htmlentities(stripslashes(firstName));
?>

最终整合

最后可以建立一个简单的函数来解决在PHP中如果安全的进行MySQL查询字符。值得注意的是,如果要输出到WEB页面上还需要使用stripslashes。



function VerifyInput(input, forceInt = false)
{
if (is_numeric(input))
{
return input;
}
elseif (!forceInt)
{
if (get_magic_quotes_gpc())
{
// if magic quotes is enabled, get rid of those
// pesky slashes
input = stripslashes(input);
}

// convert the input variable into a MySQL safe string.
input = mysql_real_escape_string(input);

return input;
}
else
{
// if input not an integer and forceInt = true,
// kill script
die("Invalid Input");
}
}

// _POST['name'] should be a string
// _POST['id'] should be an integer, if not the script dies
id = _POST['id'];
name = _POST['name'];

query = "Update users SET name=". VerifyInput(name) ." ";
query .= "Where id=". VerifyInput(id, true);

// query should be safe to run
mysql_query(query);
?>








添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.