Home > Backend Development > PHP Tutorial > Detailed explanation of login background verification example using PHP+ajax

Detailed explanation of login background verification example using PHP+ajax

墨辰丷
Release: 2023-03-28 09:48:01
Original
2223 people have browsed it

This article mainly introduces the detailed examples of PHP ajax implementation of login background verification. Interested friends can refer to it. I hope it will be helpful to everyone.

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:&#39;json&#39;,
      success:function(msg){
        //alert(msg);
        if(msg==1){
           window.location.href = "{:U(&#39;Index/personal&#39;)}";  
        }else{
          alert("登录失败,请重试!");
        }
      }
    });
});  
</script>
Copy after login

2.Back-end verification:

* */
  public function check_login(){
    $password=I(&#39;param.password&#39;);
    $username=I(&#39;param.username&#39;);
    $data["name"]=$username;
    $user=M(&#39;systemuser&#39;);
    $list=$user->where($data)->find();
    $return=0;
    if($list!=""){
      if($list[&#39;password&#39;]==md5($password) && $list[&#39;status&#39;] == 1){
        //登录时间和登录IP
        $public = new PublicController();
        $lastlogonip=$public->ip_address();
              
        $time=$time=date("Y-m-d H:i:s", time());
        $where=array(&#39;id&#39;=>$list[&#39;id&#39;]);
        
        $user->where($where)->save(array(&#39;lastlogonip&#39;=>$lastlogonip,&#39;lastlogontime&#39;=>$time));
        $this->login($list);
        $return=1;//登录成功
      }
    }else{
      $return=2;//登录失败
    }
    $this->ajaxReturn($return);
  }
Copy after login

##The above is the entire content of this article, I hope It will be helpful to everyone’s study.


Related recommendations:

PHP login (ajax submission of data and background verification)

PHP loginSession acquisition

##PHP loginRealize the remember me function

The above is the detailed content of Detailed explanation of login background verification example using PHP+ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template