Home  >  Article  >  Backend Development  >  How to implement username verification in php mysql

How to implement username verification in php mysql

PHPz
PHPzOriginal
2023-04-19 11:36:17764browse

In the process of website development, user login system is a very common function. One of the important steps is to verify whether the username and password entered by the user match, ensuring that the user enters the correct information before entering the system.

This article will introduce how to use PHP and MySQL to implement user name verification function.

  1. Create database and user table

First, we need to create a database in MySQL and create a user table in it. In this user table, we need to include the following fields:

  • id: user ID, self-increasing.
  • username: User name, unique.
  • password: User password, stored after encryption.
  • created_at: Creation time, records user registration time.

We can use the following SQL statement to create this user table:

CREATE DATABASE test;
USE test;
CREATE TABLE users (
id INT( 11) NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (id)
);

  1. Write PHP code

Next, we need to write PHP code to connect to the MySQL database and verify the username and password. The following is a simple PHP code example:

//MySQL database connection information
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "test";

// Create a MySQL connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Get the POST parameters Username and password
$username = $_POST["username"];
$password = $_POST["password"];

// Verify username and password
$sql = " SELECT * FROM users WHERE username='{$username}'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
if (password_verify($password, $row["password"])) {

echo "登录成功";

} else {

echo "密码不正确";

}
} else {
echo "Username does not exist";
}

$conn->close();
?>

The main process of the above PHP code includes:

  1. Establish a MySQL database connection for connecting and operating MySQL data. It includes four connection parameters: $servername, $username, $password, and $dbname, which are used to specify the server address, username, password and database name of the MySQL connection.
  2. Get the POST parameters submitted by the user, including username and password.
  3. Use the SQL SELECT statement to query whether there are records matching the user name in the user table. If the match is successful, use the password_verify function to verify whether the password entered by the user is consistent with the password stored in the database. What needs to be noted here is that we should use the password_hash function to encrypt the user password before storing it in the database during user registration to improve security.
  4. If the verification is successful, "Login successful" is output to the user. Otherwise, depending on the verification error, "Password is incorrect" or "Username does not exist" is output.
  5. Create user registration page

In order to simplify the process of user registration and adding new user records in the database, we can create a user registration page. In this page, we will ask the user to fill in their username and password, and pass this information to the PHP code we wrote previously through the POST method.

The following is an example of a simple user registration page:



User Registration Page


User Registration


<label for="username">用户名:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="注册">



In the above code, we use the

tag to create a form to collect users The username and password entered during the registration process. Among them, the user name is entered using the text box, and the password is entered using the password box.

  1. Summary

In this article, we introduced how to use PHP and MySQL to implement the username verification function. We first created a MySQL database and a users table, and created a unique username and encrypted password for each user record. We then wrote a PHP code to connect to the MySQL database and check if the username and password entered by the user match. Finally, we created a user registration page that allows users to register a new account in a custom form.

The above is what this article introduces. I hope it can help you realize your user verification function.

The above is the detailed content of How to implement username verification in php mysql. 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