First step
(1) Form login settings and form validation
1> Set click event - > get the password value of the account entered in the form - > use regular expression to verify the form - > send ajax request - > enter the background - > Import configuration file - > receive the data sent by ajax - > connect to the database - > write sql statement - > query the database - > judge the query result - > assume the query succeeds - > open session ﹐ start() -- store the background Data - > set the response type (in json format) - > output data - > the browser can judge whether the request is successful by obtaining the data stored in the background -- > if the request is successful, jump to the page -- > jump to the page again to verify whether the login is successful.
<script> //Verify the form on the browser side first //Click event of login button registration $("#btn-login").on("click",function(){ //1. Collect the password of the user's mailbox var email=$("#email").val(); var password=$("#password").val(); //2. Data validation / / form validation on browser side var reg=/\w+\@\w+\.\w+/; if(!reg.test(email)){ $("#msg").text(" there is an error in the information you entered, please re-enter "); $(".alert").show(); return; } //Form password length var pwdReg=/\w{3,20}/; if(!pwdReg.test(password)){ $("#msg").text('wrong password length, please re-enter '); $(".alert").show(); return; }- $.ajax({ type:"post", data:{ email : email, password : password}, url:"api/_userLogin.php", success:function(res){ if(res.code==1){ alert('Login successfully'); // If the connection successfully jumps to the background page location.href='index.php'; }else{ $("#msg").text('wrong user name or password '); $(".alert").show(); } } }); }); </script>
<?php require_once '../../config.php'; require_once '../../functions.php'; // Backstage // Receive email and password from ajax $email=$_POST['email']; $password=$_POST['password']; // Query whether the database has this mailbox and password // Connect to database $connect=connect(); $sql="select * from users where email='{$email}' and password='{$password}' and status='activated'"; // Implementation of sql $queryResult=query($connect,$sql); //Two dimensional array has a found user $response=["code"=>0,"msg"=>"operation failed"]; // If no one returns to failure // If there's evidence of this person, the login is successful if($queryResult){//Login successfully //Login successful remember he's logged in session_start();//Be sure to turn it on first $_SESSION['isLogin']=1;//The value of isLogin is 1 for login // Remember that the id of the currently logged in person will be used later $_SESSION['user_id']=$queryResult[0]['id']; $response["code"]=1; $response["msg"]="Login successfully"; } // Return in json format header("content-type:application/json;charset=utf8"); echo json_encode( $response ); ?>