*******php项目中当我们要对数据库进行写入操作时,有时会因为代码没有做防sql注入工作,导致各种不可预知的错误*******
1,index.htm 这是一个很简单的注册页面l 我这是以ajax形式提交数据
复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RegisterForm</title>
<script type="text/javascript" src='jquery-1.3.1.js'></script>
</head>
<body>
<h1>This is a test form! </h1>
<font color="#8b0000"><table cellpadding="5" cellspacing=0>
<form action='_process.php' method='post' enctype='multipart/form-data'>
<tr><td><em>user :</em></td><td><input type='text' value='' size=15/></td></tr>
<tr><td><em>ages : </em></td><td><input type='text' value='' size=15/></td></tr>
<tr><td><em>pass : </em></td><td><input type='password' value='' size=15/></td></tr>
<tr><td><em>addr : </em></td><td><input type='text' value='' size=15/></td></tr>
<tr><td><em>email : </em></td><td><input type='text' value='' size=15/></td></tr>
<tr><td><input type='button' value='register'/></td><td><input type='reset' value='reseting'/></td></tr>
</form>
</table></font>
<span>正在加载... ...</span>
<p></p>
<style type="text/css">
body{text-align:center;}
.error{color:red;}
.tb{margin:0 auto;width:350px;height:200px;text-align:center;}
.tdleft{width:150px;text-align:left;}
</style>
<script type='text/javascript'>
$("#but").click(function(){
var name = $.trim($("#name").val());
var ages = $.trim($("#ages").val());
var pn = /^/d+$/;
var addr = $.trim($("#addr").val());
var pass = $.trim($("#password").val());
var email = $.trim($("#email").val());
var reg = /^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+(/.[a-zA-Z]+)+$/gi;
if(name.length==0){
alert("请认真填写姓名!");return false;
}
if(ages.length==0){
alert("请填写年龄!");return false;
}
if(!pn.test(ages)){
alert("请填写有效数字!");return false;
}
if(pass.length==0||pass.length>6){
alert("请认真填写密码!");return false;
}
if(addr.length==0){
alert("请认真填写地址!");return false;
}
if(email.length==0){
alert("请认真填写邮件!");return false;
}
if(!reg.test(email)){
alert("电子邮件检测失败");return false;
}
var send = {'name':name,'ages':ages,'pass':pass,'addr':addr,'email':email};
$.post('_process.php',send,function(data){
if(data.res==-1){
$("#result").addClass("error");
}
$("#result").html(data.msg);
//$("form")[0].reset();
},'json');
})
$("#msg").ajaxStart(function(){
$(this).fadeIn();
}).ajaxStop(function(){
$(this).fadeOut();
})
</script>
</body>
</html>
复制代码 代码如下:
<?php
header('Content-Type:text/html;charset=utf-8');
sleep(1);
$conn = mysqli_connect("localhost","root","root","register") or die("Unable to connect!".mysqli_connect_error());
mysqli_query($link,"set names utf8");
$name = isset($_POST['name'])?mysqlQuotes(trim($_POST['name'])):'';
$ages = isset($_POST['ages'])?intval(trim($_POST['ages'])):'';
$pass = isset($_POST['pass'])?mysqlQuotes(trim($_POST['pass'])):'';
$addr = isset($_POST['addr'])?mysqlQuotes(trim($_POST['addr'])):'';
$email = isset($_POST['email'])?mysqlQuotes(trim($_POST['email'])):'';
if(empty($name)){
echo json_encode(array('res'=>-1,'msg'=>'请您认真输入姓名'));exit;
}
//查询username在 tb_register 表中是否存在,不存在则入库,存在则告知已注册
$sql0 = "select count(1) as nums from tb_register where username='{$name}'";
$row0 = select_one($conn,$sql0);
if($row0['nums']>=1){
echo json_encode(array('res'=>-1,'msg'=>'对不起,该用户名已被注册!'));exit;
}
if(empty($ages)){
echo json_encode(array('res'=>-1,'msg'=>'请您认真输入年龄'));exit;
}
if(!preg_match("/^/d+$/",$ages)){
echo json_encode(array('res'=>-1,'msg'=>'请输入合法的数字'));exit;
}
if(empty($pass)){
echo json_encode(array('res'=>-1,'msg'=>'请您认真输入密码'));exit;
}
if(empty($addr)){
echo json_encode(array('res'=>-1,'msg'=>'请您认真输入地址'));exit;
}
if(empty($email)){
echo json_encode(array('res'=>-1,'msg'=>'请您认真输入邮箱'));exit;
}
if(!preg_match("/^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+(/.[a-zA-Z]+)+$/",$email)){
echo json_encode(array('res'=>-1,'msg'=>'您的邮箱格式不正确'));exit;
}
$add_day = date("YmdHis");
//以下是入库操作
$sql = "insert into tb_register set username='{$name}',ages='{$ages}',password='{$pass}',address='{$addr}',email='{$email}',add_day='{$add_day}'";
mysqli_query($conn,$sql);
echo json_encode(array('res'=>1,'msg'=>'恭喜你,注册成功!'));exit;
//预防mysql注入攻击函数 入库数据
function mysqlQuotes($content){
if(!get_magic_quotes_gpc()) //先看看是否开启自动过滤机制 未开启则手动加函数过滤
{
$content = addslashes($content);
}
return stripslashes(htmlspecialchars($content));
}
function select_one($conn,$sql){
$res = mysqli_query($conn,$sql) or die("Failed".mysqli_error());
$result = array();
if(!empty($res)){
$result = mysqli_fetch_assoc($res);
}
mysqli_free_result($res);
return $result;
}
新闻热点
疑难解答
图片精选