网站建设
  简约型网页设计套餐998
  实惠型网站建设套餐2580
  综合型网站制作套餐4980
  网站改版与网站维护
  行业网站建设方案
  大型网站建设解决方案
  企业网站建设流程
  帝网科技网站设计与网站制作
建站FAQ
·网站空间问题解答
·企业邮箱问题解答
 
酷站欣赏
·房产酷站(379)
·综合门户(8 9)
·建筑装饰(603)
·手机通讯(354)
·生活购物(376)
·医疗保健(199)
·文化摄影(602)
·休闲体育(399)
>>更多酷站欣赏
网站优化
·Google(谷歌)优化   ·百度(BaiDu)优化
·雅虎(Yahoo)优化    ·Alexa排名优化   
·Google AdSense   ·DMOZ目录提交  
建站知识
·网站建设知识·网站名词解释·网站运营知识
·网络营销知识·搜索引擎知识·实用技术文摘
网站推广
百度网站推广 google网站推广
搜狐网站推广 网易网站推广
新浪网站推广   雅虎网站推广
  您当前位置: 当前位置:帝网科技 >> web开发 >> PHP专栏 >> 浏览文章
 
 
PHP与MySQL中的SQL注入式漏洞
作者:袁袁整理 来源:帝网科技 日期:2008年10月20日 点击数:


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);
?> 

  相关文章
 
·如何在PHP开启gzip页面压缩实例正文分
·如何正确运用PHP json_encode函数进行
·PHP下载断点续传的代码正文分析错误
·PHP取得客户端IP地址代码正文分析错误
·PHP判断用户IP来路的一个方法正文分析
·PHP+mysql分页代码正文分析错误
·php去除HTML标记正文分析错误
·php轻松快速缓存全站正文分析错误
·php多语言网站解决方案正文分析错误
·php图片验证码函数正文分析错误
·PHP初学者常见问题集(21问答)正文分
·分享PHP技术开发技巧正文分析错误
·cURL库功能简介:抓取网页 POST数据及
·PHP常用函数:过滤HTML字符串正文分析
·php fscanf()函数使用方法详解正文分析
·和php有关的几种常见安全详解正文分析
·php连接mysql出现乱码解决办法正文分析
·PHP非常简单的使用模板制作静态页面正
·非常简单PHP缩略图生成程序源代码正文
·无法载入 mcrypt 扩展,请检查 PHP 配置
 
 

公司环境 | 合作伙伴 | 人才招聘 | 付款方式 | 关于我们

地址:广州市天河区中山大道中120号D805 电话:020-82529556 传真:020-82529556
广州帝网网络科技有限公司 版权所有 粤ICP备08119341号