"PHP additions and Deletions Example" section 18th-login.php writing

Source: Internet
Author: User
Tags sleep function

1. Non-null judgment of user name and password (background verification)

    $username;    $password;    if(isset($_POST[‘username‘]) && $_POST[‘username‘] != null){        $username = $_POST[‘username‘];    }else{        echo "

2. Make a database link

$conn = mysql_connect("localhost","root","");$db = mysql_select_db("test",$conn);mysql_query("set names utf8");
  1. The
  2. Queries the user table based on the user name and password, and if the user exists, the number of queries is definitely equal to 1, otherwise the user name and password are considered wrong. There is also a situation, is simply based on the user name to check, if the user name is not found, the user is prompted that the user name does not exist.

      $sql = "SELECT count (*) as total from tm_users where username = ' $username '", $rs = mysql_query ($sql); while ( $row = mysql_fetch_array ($rs)) {if ($row ["Total"]! = 1) {echo "

  3. Asynchronous Login
    In the previous section, we used the form form submission method, which is commonly known as synchronous landing. This way, if the background does not return data, then the user can not do anything. Now, we use the sleep function to simulate the problem of too long background execution time.

    sleep(5000);


    If because of the network and other problems, the background processing requests too long, the user in addition to wait, nothing can be done. This is one of the drawbacks of synchronous submissions.

    , synchronization is similar to the phone, if the other person does not answer, the caller can only wait, do not want to wait, put the phone hung. If it's an asynchronous way, like two people texting, when you reply, I'll see when it won't affect anything I do in the current period.

Now to demonstrate asynchronous commits.

16.1 Copy the login2.html to this directory:

C:\xampp\htdocs\5-7\login

Open Browser, access address:http://localhost:8080/5-7/login/login2.html


User name and password non-null judgment, in the foreground JS also have processing. If neither the user name nor the password is empty, then asynchronous commits are made, and the asynchronous commit takes the Ajax method on line No. 281:

16.2 Writing login2.php

Check the successful login test first:

<?php    $resultData = array();    $resultData["errCode"] = 0;    $resultData["errMsg"] = "";    echo json_encode($resultData);?>

Do nothing, return directly to the result object without errors, and login2.html will enter the success method:

Here also made a small change, when the login success, go to visit main.html.

16.3 background non-null judgment
//后台判断用户名和密码是否为空    $username;    $password;    if(isset($_POST[‘account‘]) && $_POST[‘account‘] != null){        $username = $_POST[‘account‘];    }else{        $resultData["errCode"] = -1;        $resultData["errMsg"] = "用户名不能为空!";        echo json_encode($resultData);        return;    }    if(isset($_POST[‘password‘]) && $_POST[‘password‘] != null){        $password = md5($_POST[‘password‘]);    }else{        $resultData["errCode"] = -1;        $resultData["errMsg"] = "密码不能为空!";        echo json_encode($resultData);        return;    }
16.4 Verify that the user name exists?
    //验证用户名是否存在?    $sql = "select count(*) as total from tm_users where username = ‘$username‘";    $rs = mysql_query($sql);    while($row = mysql_fetch_array($rs)){        if($row["total"] != 1){            $resultData["errCode"] = -1;            $resultData["errMsg"] = "该用户名不存在!";            echo json_encode($resultData);            return;        }    }
16.5 Verify that the user name and password are correct?
//验证用户名和密码是否正确?    $sql = "select count(*) as total from tm_users where username = ‘$username‘ and password = ‘$password‘";    $rs = mysql_query($sql);    while($row = mysql_fetch_array($rs)){        if($row["total"] != 1){            $resultData["errCode"] = -1;            $resultData["errMsg"] = "用户名或者密码错误!";            echo json_encode($resultData);            return;        }    }

SOURCE Access: HTTPS://WWW.JIANSHU.COM/P/4977BD0073D5

"PHP additions and Deletions Example" section 18th-login.php writing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.