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

Implement user login and registration system using PHP

王林
Release: 2023-05-11 08:38:01
Original
3317 people have browsed it

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)
Copy after login

);

  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:



<title>用户注册</title>
Copy after login

< ;/head>

<h1>用户注册</h1>
<form action="register.php" method="post">
    <label>用户名:</label>
    <input type="text" name="username" required>
    <br><br>
    <label>密码:</label>
    <input type="password" name="password" required>
    <br><br>
    <input type="submit" value="注册">
</form>
Copy after login


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:

// Get user input
$username = $_POST['username'];
$password = $_POST[ 'password'];

// Test database connection
$mysqli = new mysqli('localhost', 'root', '', 'test');
if ($mysqli-> ;connect_errno) {

die('连接失败: '.$mysqli->connect_error);
Copy after login
Copy after login

}

// Check if the username is unique
$result = $mysqli->query("SELECT * FROM users WHERE username='$username' ");
if ($result->num_rows > 0) {

die('用户名已存在');
Copy after login

}

// 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 '注册成功';
Copy after login

} else {

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

}
?>

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:



<title>用户登录</title>
Copy after login

< ;/head>

<h1>用户登录</h1>
<form action="login.php" method="post">
    <label>用户名:</label>
    <input type="text" name="username" required>
    <br><br>
    <label>密码:</label>
    <input type="password" name="password" required>
    <br><br>
    <input type="submit" value="登录">
</form>
Copy after login


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:

// Get user input
$username = $_POST['username'];
$password = $_POST[ 'password'];

// Test database connection
$mysqli = new mysqli('localhost', 'root', '', 'test');
if ($mysqli-> ;connect_errno) {

die('连接失败: '.$mysqli->connect_error);
Copy after login
Copy after login

}

// Check if the username exists
$result = $mysqli->query("SELECT * FROM users WHERE username='$username' ");
if ($result->num_rows == 0) {

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

}

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

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

}

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!

Related labels:
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