Home > Article > Web Front-end > PHP login (ajax submission data and background verification)
This article mainly shares the example code of PHP login (ajax submission data and background verification), which has certain reference value. Let’s take a look at it together
1. Front-end ajax data submission
<form id="login_form" action="" method="POST"> <p class="login_frame" style="position:relative";> <p class="login_gl" style="margin-top:35px;"> <span class="login_wz" >后台管理系统</span> </p> <p class="login_user"> <input id="username" name="username" type="text" placeholder="请输入您的用户名" value="" style="width:100%;height:32px;border-style:none;font-size:16px;color:#959595;"/> </p> <p class="login_user"> <input id="password" name="password" type="password" placeholder="请输入您的密码" value="" style="width:100%;height:32px;border-style:none;font-size:16px;color:#959595;"/> </p> <p id="login_btn" class="login_log"> <span style="font-size:16px;">登录</span> </p> </p> </form> </p> <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. Background verification:
* */ 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); }
#The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
AJAX implements the function of detecting user names without refreshing
JQuery calls webservice through ajax to pass array parameters Problem (graphic tutorial)
Detailed explanation of the method of using ajax to pass arrays and receive in the background
The above is the detailed content of PHP login (ajax submission data and background verification). For more information, please follow other related articles on the PHP Chinese website!