Home  >  Article  >  Backend Development  >  Implement user login and registration system using PHP

Implement user login and registration system using PHP

王林
王林Original
2023-05-11 08:13:413048browse

With the widespread application of the Internet, user login and registration systems have also become essential functions for many websites. In this article, we will implement a simple user login and registration system through PHP language.

  1. Build a database

First, we need to create a database to store user information. We will use a MySQL database and create a table called "users" to store usernames and passwords.

CREATE TABLE users (

id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL,
PRIMARY KEY (id)

);

  1. Registration page

Next, we need to create a registration page, use to ask the user to enter their username and password. In the registration page, we need to use an HTML form to collect the user's input information and use PHP code to insert this information into the database.

The following is the code for a simple registration page:

8b05045a5be5764f313ed5b9168a17e6
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e

用户注册

< ;/head>
6c04bd5ca3fcae76e30b72ad730ca86d

用户注册





36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

In the above code, we use an HTML form to collect The user enters the information and sets the form's action attribute to "register.php", which means that when the user submits the form, the PHP code processes the input information. Next, we will write the register.php file to handle these inputs.

  1. Registration Processing

Before inserting form data into the database, we need to ensure that the input data is valid. We verify that usernames are unique and passwords are encrypted. The following is the code of the register.php file:

bdc51052a0ae7835bbef76a014c17443query("SELECT * FROM users WHERE username='$username' ");
if ($result->num_rows > 0) {

die('用户名已存在');

}

// Encrypted password
$password = password_hash($password, PASSWORD_DEFAULT) ;

// Insert user information into the database
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if ($mysqli ->query($sql)) {

echo '注册成功';

} else {

echo '注册失败: '.$mysqli->error;

}
?>

In the register.php file above, we First get the username and password entered by the user. Next, we check if the username already exists in the database. Then, we use the password_hash function to encrypt the password. Finally, we insert the username and encrypted password into the database. If the insertion is successful, we will output a "Registration Successful" message. Otherwise, we will output a "Registration failed" message.

  1. Login page

Next, we will create a login page for users to enter their registered username and password. In the login page, we will use an HTML form to collect the user's input information and PHP code to authenticate the user's identity.

The following is the code for a simple login page:

8b05045a5be5764f313ed5b9168a17e6
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e

用户登录

< ;/head>
6c04bd5ca3fcae76e30b72ad730ca86d

用户登录





36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

In the above code, we use an HTML form to collect The user's input information, and the form's action attribute is set to "login.php", which means that when the user submits the form, the PHP code processes the input information. Next, we will write the login.php file to verify the user's identity information.

  1. Login processing

Before processing the user's login request, we need to ensure that the entered data is valid. We will check if the username exists and if the password entered matches the password in the database. The following is the code of the login.php file:

7fcb39b31659f4a7f047642795718fdbquery("SELECT * FROM users WHERE username='$username' ");
if ($result->num_rows == 0) {

die('用户名不存在,请检查输入');

}

// Check if the password is correct
$user = $result-> fetch_assoc();
if (!password_verify($password, $user['password'])) {

die('密码错误,请检查输入');

}

echo 'Welcome'.$username;
?>

In the login.php file above, we first obtain the username and password entered by the user. Next, we check if the username exists in the database. We then get that user's password from the database and use the password_verify function to check if the password entered matches the password in the database. If the passwords match, a welcome message is output. Otherwise, we print an error message and terminate the program.

So far, we have completed the implementation of a simple user login and registration system. This system allows users to register an account on the website and log in in order to access specific pages. Of course, this is only the most basic implementation method. In actual applications, more complex functions are needed to ensure the availability and security of the system.

The above is the detailed content of Implement user login and registration system using 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