当前位置: 首页 > 图文教程 > 网络编程 > PHP > php SQL防注入代码

PHP
[PHP]实用函数3
[PHP]实用函数4
[PHP]实用函数5
[PHP]实用函数6
[PHP]实用函数7
[PHP]实用函数8
[PHP]实用函数9
[PHP]实用函数10
表单复选框向PHP传输数据的代码
PHP编程中字符串处理的5个技巧小结
php Mysql日期和时间函数集合
分页详解 从此分页无忧(PHP+mysql)
PHP 文件上传进度条的两种实现方法的代码
php a simple smtp class
利用PHP制作简单的内容采集器的代码
php代码把全角数字转为半角数字
php获得当前的脚本网址
php字符串截取中文截取2,单字节截取模式
php下过滤HTML代码的函数
修改php.ini实现Mysql导入数据库文件最大限制的修改方法

PHP 中的 php SQL防注入代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 108 ::
收藏到网摘: n/a

php下实现sql防注入效果代码,asp的比较多,php的倒不多见,喜欢php的朋友可以参考下 SQL防注入代码
<?php
/**
* 防sql注入
* @author: [email protected]
* */
/**
* reject sql inject
*/
if (!function_exists (quote))
{
function quote($var)
{
if (strlen($var))
{
$var=!get_magic_quotes_gpc() ? $var : stripslashes($var);
$var = str_replace("'","\'",$var);
}
return "'$var'";
}
}
if (!function_exists (hash_num)){
function hash_num($input)
{
$hash = 5381;
for ($i = 0; $i < strlen($str); $i++)
{
$c = ord($str{$i});
$hash = (($hash << 5) + $hash) + $c;
}
return $hash;
}
}
/**************** end *************************/
?>


<?php
/**
* 防sql测试代码
CREATE TABLE IF NOT EXISTS `tb` (
`id` int(10) unsigned NOT NULL auto_increment,
`age` tinyint(3) unsigned NOT NULL,
`name` char(100) NOT NULL,
`note` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
**/
include_once('common.php');
var_dump(hash_num('dddd'));
if(empty($_GET))
{
$_GET = array('age'=>'99','name'=>'a\'b\\\'c";','note'=>"a'b\'\nc#");
}
$age = (int)$_GET['age'];
$name = quote($_GET['name']);
$note = quote($_GET['note']);
$sql = "INSERT INTO `tb` ( `age`, `name`, `note`) VALUES
( $age, $name, $note)";
var_dump($sql);
?>