作者:不需要忆jf | 来源:互联网 | 2023-09-17 22:15
html代码,login.html
jquery代码,login.js
$('img').click(function(){
$('img').attr('src','php/getVerify.php?'+Math.random());//刷新验证码
})
$('#login').click(function(){
var username=$('#user-name').val();
var password=$("#user-password").val();
var code=$("#code").val();
if(username!=""&&password!=""&&code.length==4){
$.ajax({
type:"POST",
url:"php/userLogin.php",
dataType:"JSON",
data:{
"user_name":username,
"password":password,
"code":code
},
success:function(data){
switch(data){
case 1://普通用户
$.COOKIE("user",username);
$.COOKIE("limit",0);
window.location.href="index.php";
break;
case 2://管理员用户
$.COOKIE("user",username);
$.COOKIE("limit",1);
window.location.href="index.php";
break;
case 3://密码错误
alert("密码错误!");
break;
case 4://用户不存在
alert("该用户不存在!");
break;
case 0://验证码错误
alert("验证码不正确!");
break;
}
}
})
}else{
alert("请检查您的输入!");
}
})
$('#sign').click(function(){
var username=$('#user-name').val();
var password=$("#user-password").val();
var code=$("#code").val();
if(username!=""&&password!=""&&code.length==4){
$.ajax({
type:"POST",
url:"php/addUser.php",
dataType:"JSON",
data:{
"user_name":username,
"password":password,
"code":code
},
success:function(data){
switch(data){
case 1://用户已存在
alert("该用户已存在!请换一个用户名注册。")
break;
case 2://注册成功
alert("注册成功!");
$.COOKIE("user",username);
$.COOKIE("limit",0);
window.location.href="index.php";
break;
case 0://验证码错误
alert("验证码不正确!");
break;
}
}
})
}else{
alert("请检查您的输入!");
}
})
php代码,userLogin.php
header("Content-type: text/html; charset=UTF-8");
session_start();
$name = $_POST['user_name'];
$password=$_POST['password'];
$code=$_POST['code'];
$con=mysql_connect('localhost','root','');
if(!$con){
die('error:'.mysql_error());
}
mysql_select_db('db_name');
$result=mysql_query("select * from users where user_name='$name'");
if($_SESSION['verify']==$code){
if($row=mysql_fetch_array($result)){
if($row['password']==$password){
if($row['power']==0){
echo 1;//普通用户
}else{
echo 2;//管理员用户
}
}else{
echo 3;//密码错误
}
}else{
echo 4;//用户不存在
}
}else{
echo 0;//验证码错误
}
addUser.php
header("Content-type: text/html; charset=UTF-8");
session_start();
$name = $_POST['user_name'];
$password=$_POST['password'];
$code=$_POST['code'];
$con=mysql_connect('localhost','root','');
if(!$con){
die('error:'.mysql_error());
}
mysql_select_db('db_name');
$result=mysql_query("select * from users where user_name='$name'");
if($_SESSION['verify']==$code){
if($row=mysql_fetch_array($result)){
echo 1;//用户已存在
}else{//注册成功
mysql_query("insert into `users` (`user_name`,`password`) values ('$name','$password')");
echo 2;
}
}else{
echo 0;
}