页面导航: 首页网络编程PHP编程php技巧 → 正文内容 php SQL 防注入

php SQL防注入代码

发布:dxy 发布日期:2008-4-25 15:13:25 字体:[增加 减小] 类型:转载
php下实现sql防注入效果代码,asp的比较多,php的倒不多见,喜欢php的朋友可以参考下
SQL防注入代码 
<?php
/**
* 防sql注入 
* @author: zhuyubing@gmail.com
* */
/**
* 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);
?>
Tags:php SQL 防注入
百度中搜索更多的关于php SQL防注入代码内容,或者用Google搜索相关更多
浏览次数:载入中... 打印本文关闭本文返回首页

文章评论

共有 位脚本之家网友发表了评论我来说两句

同 类 文 章
最 近 更 新
热 点 排 行