Home  >  Article  >  Backend Development  >  How to determine login failure in php

How to determine login failure in php

WBOY
WBOYOriginal
2023-05-06 13:55:07606browse

As a scripting language, php plays a vital role in the development of many website applications. In many applications, the login system is also an almost indispensable part. Whether it is a shopping mall, social networking site or community website, user login is one of the essential processes. Therefore, when developing such an application, it is crucial to determine whether the login is successful or failed. This is the primary condition for protecting user information and application security.

When a user enters a username and password, how to determine whether the user's login is successful or failed? Below I will answer this question from the perspective of php program.

  1. Form verification

When the user submits the form, we first need to verify the data submitted by the user to ensure the correctness of the form input. Here is the first step in determining whether the user has failed to log in. We can implement form verification by writing corresponding PHP code, such as the following code:

if($_POST['username'] == ''){
    echo "请输入用户名";
}elseif($_POST['password'] == ''){
    echo "请输入密码";
}else{
    //验证表单通过
}

Through the above code, we can determine whether the user has entered the user name and password. If the user does not enter one of the items, a corresponding error message will be prompted. However, just determining whether the user has entered the user name and password is not enough to determine whether the user's login is successful.

  1. Database Verification

After the user submits the form, we also need to verify in the background whether the user's username and password are correct. This needs to be linked to the user table in the database for verification. Here we assume that the username and password are stored in the username and password fields of the user table respectively.

After establishing a database connection with the mysql database, we can retrieve the username and password that exist in the database through the following code:

$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM 用户表格 WHERE username='$username'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);

Then compare it with the username and password entered by the user, if the username and password If correct, we consider the user's login successful; otherwise, if the username and password are incorrect, we consider the user's login failed.

if($row && $row['password']==$password){
    echo "登录成功";
}else{
    echo "登录失败";
}
  1. Add login restrictions

After determining whether the user login is successful, we must also consider the situation where some users log in maliciously. For example, if a user uses a blasting tool to make a large number of invalid login attempts, this can cause excessive burden on the server. Therefore, we should also add login restrictions during the login process to control the number of invalid login attempts.

To achieve this, we can set a counter variable in the program, and whenever there is a malicious login attempt, the counter will be incremented. When the counter reaches the set value, you can set some restrictive measures, such as temporarily prohibiting the user from logging in for a period of time.

//设定最多尝试登录次数
$max_login_times = 3;
$login_failed = false;
//获取用户输入的用户名和密码
$username = $_POST['username'];
$password = $_POST['password'];
//从数据库中获取用户名对应的密码
$sql = "SELECT * FROM `user` WHERE username='$username'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);

//如果用户输入的密码与数据库中密码不一致,或者数据库中没有该用户名
if(!$row || $row['password']!=$password){
    $login_failed = true; //登录失败
    //每次登录失败,则计数器增加一
    $count = isset($_SESSION['login_count'])?$_SESSION['login_count']:0;
    $_SESSION['login_count'] = ++$count;

    if($count>=$max_login_times){
        //登录失败次数过多,锁定用户一段时间
        $lock_time = 60*60;
        $_SESSION['lock_time'] = time()+$lock_time; //锁定时间为一小时
    }

}else{
    session_start();
    //用户登录成功
    $_SESSION['user_id']=$row['id'];
    $_SESSION['user_name']=$row['username'];
}

The above code is a very simplified login.php file, which completes the implementation of a login failure counter of up to three times. Each failed login attempt will increase the counter by one. When the counter reaches the maximum value, this IP will be restricted from trying to log in for a period of time. When the password matches the password in the database, the user session is successfully created for later use.

In short, no matter what application it is, the correctness and security of user login system verification have always been one of the key issues in application development. Only through multiple protection measures such as form verification, database verification, and login restrictions can we more comprehensively and effectively determine the situation of user login failure.

The above is the detailed content of How to determine login failure in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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