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.
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)
);
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>
< ;/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>
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.
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);
}
// Check if the username is unique
$result = $mysqli->query("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.
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>
< ;/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>
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.
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);
}
// Check if the username exists
$result = $mysqli->query("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!