Home > Backend Development > PHP Problem > How PHP uses arrays to implement user login function

How PHP uses arrays to implement user login function

PHPz
Release: 2023-04-24 15:46:49
Original
623 people have browsed it

With the development of the Internet, the user login function of the website has become more and more popular. User login is a very important part of modern websites as it protects users' privacy and provides them with secure access. In this article, we will learn how to use arrays in PHP to implement a simple user login function.

First, we need to understand how to use PHP to create a basic web page and embed HTML tags in it. The following is a simple PHP code example:

<!DOCTYPE html>
<html>
<head>
    <title>登录</title>
</head>
<body>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <label for="username">用户名:</label>
        <input type="text" name="username" id="username"><br/><br/>
        <label for="password">登录密码:</label>
        <input type="password" name="password" id="password"><br/><br/>
        <input type="submit" name="submit" value="登录">
    </form>
</body>
</html>
Copy after login

The above code will generate a basic user login web page, which contains a form where the user can enter their username and password. This form is submitted using the POST method to prevent sensitive information from being exposed by the URL. In addition, we also use the htmlspecialchars() function in PHP after the form is submitted to prevent malicious users from attacking the security of our application by entering HTML tags.

Next, we will set up our user data. For simplicity, we will use a PHP array containing username and password as shown below:

$users = array(
    array("username" => "admin", "password" => "password123"),
    array("username" => "test", "password" => "test123"),
    array("username" => "guest", "password" => "guest123"),
);
Copy after login

In real applications, we usually store user data in a database for more secure management and operate.

Now that we have our user data and login form set up, we need to write PHP code to verify that the username and password entered by the user match the user data we defined. To do this, we can create a function called "verifyLogin" after the form is submitted, as shown below:

function verifyLogin($username, $password, $users) {
    foreach($users as $user) {
        if ($user["username"] == $username && $user["password"] == $password) {
            return true;
        }
    }

    return false;
}
Copy after login

The above function will receive the username and password entered by the user, as well as the user array we defined earlier. Inside the function, we loop through the array to check if the entered username and password match any of the user data we defined. If the match is successful, it returns true directly, otherwise it returns false.

Finally, we also need to pass the user name and password entered by the user to the verifyLogin() function for verification after the form is submitted. In the form HTML, we can set the action attribute of the form tag to the name of the current PHP file and the method attribute to "POST". In this way, we can obtain the username and password entered by the user through the $_POST global variable after the form is submitted. Here is an example:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST["username"];
    $password = $_POST["password"];

    if (verifyLogin($username, $password, $users)) {
        echo "登录成功!";
    } else {
        echo "用户名或密码错误!";
    }
}
Copy after login

The above code will use the $_POST global variable to get the username and password entered by the user after the form is submitted, and then pass them to the verifyLogin() function for verification. If the verification is successful, the message "Login successful!" will be output, otherwise the message "Username or password is incorrect!" will be output.

In summary, we have learned how to use arrays in PHP to implement a simple user login function. Although our sample code is very basic, for PHP developers who are just getting started, it is enough to help them understand and master the application of arrays in PHP. Of course, in real application scenarios, we need to consider more reliable and secure login methods, such as using password hashing, salting and other technologies to protect the security of user data.

The above is the detailed content of How PHP uses arrays to implement user login function. For more information, please follow other related articles on the PHP Chinese website!

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