1.前台ajax数据提交
<form action="" method="POST"> <div;> <div> <span >后台管理系统</span> </div> <div> <input type="text" placeholder="请输入您的用户名" value=""/> </div> <div> <input type="password" placeholder="请输入您的密码" value=""/> </div> <div> <span>登录</span> </div> </div> </form> </div> <script type="text/javascript"> $("#login_btn").click(function(){ var username = $.trim($("#username").val()); var password = $.trim($("#password").val()); if(username == ""){ alert("请输入用户名"); return false; }else if(password == ""){ alert("请输入密码"); return false; } //ajax去服务器端校验 var data= {username:username,password:password}; $.ajax({ type:"POST", url:"__CONTROLLER__/check_login", data:data, dataType:'json', success:function(msg){ //alert(msg); if(msg==1){ window.location.href = "{:U('Index/personal')}"; }else{ alert("登录失败,请重试!"); } } }); }); </script>
2.后台校验:
* */ public function check_login(){ $password=I('param.password'); $username=I('param.username'); $data["name"]=$username; $user=M('systemuser'); $list=$user->where($data)->find(); $return=0; if($list!=""){ if($list['password']==md5($password) && $list['status'] == 1){ //登录时间和登录IP $public = new PublicController(); $lastlogonip=$public->ip_address(); $time=$time=date("Y-m-d H:i:s", time()); $where=array('id'=>$list['id']); $user->where($where)->save(array('lastlogonip'=>$lastlogonip,'lastlogontime'=>$time)); $this->login($list); $return=1;//登录成功 } }else{ $return=2;//登录失败 } $this->ajaxReturn($return); }