1.login.html?
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<script src="/TpPublic/youlipinM/js/zepto.min.js"></script>
<script src="/TpPublic/youlipinM/js/zhuce.js"></script>
<script src="/TpPublic/youlipinM/js/adaptation.js"></script>
<body>
<form>
用户名 <input type="text" name="username" id="username" ><br>
密码 <input type="password" name="pass" id="pass" ><br>
<input type="button" value="登录" οnclick="checkLogin()">
<div class="tishi"><b class="close"></b><p class="err"></p></div>
</form>
</body>
<script>
function checkLogin(){
var username =$("#username").val();
var reg = /^\w{3,}$/;
var pass = $("#pass").val();
if(username =="" ||!reg.test(username)){
$('.err').html('您的用户名有误');
$('.tishi').show();
return false;
};
if(pass==''){
$('.err').html('密码有误');
$('.tishi').show();
return false;
};
// 使用ajax传用户名密码 到后台控制器
$.ajax({
type:"post",
url:"{:U('Logins/login')}",
data:{'username':username,'pass':pass},
success:function (msg){
if(msg =='0'){
alert('登录成功');
window.location.href = "{:U('Index/index')}";
}else{
alert('登录失败');
window.location.href = "{:U('Logins/login')}";
}
}
})
}
</script>
</html>
2.控制器? ?LoginsController.class.php??
<?php
namespace akszx\Controller;
use Think\Controller;
class LoginsController extends Controller{
public function login(){
if(IS_AJAX){
$username = I('username');
$pass = I('pass');
$users = M()->table('kehu_users')->where(array('username'=>$username,'pass'=>md5($pass)))->find();
if(users){
$this->ajaxReturn(0);exit;
}else{
$this->ajaxReturn(1);exit;
}
}
$this->display();
}
}
2021/08/05 收藏
|