PHP Development Guide: How to implement user registration and login functions
In modern Web applications, user registration and login functions are indispensable basic functions. This article will introduce how to use PHP development to implement user registration and login functions, and attach corresponding code examples.
First, we need to create a user table to store registered user information. Suppose our table is named "user" and contains the following fields:
id - user ID (auto-incremented primary key)
username - username
password - password (encrypted and stored)
email - Email
The SQL statement to create the table is as follows:
CREATE TABLE `user` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(50) NOT NULL, `password` VARCHAR(50) NOT NULL, `email` VARCHAR(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`), UNIQUE KEY `email` (`email`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Next, we implement the user registration function. After the user submits the registration form, we need to verify the legitimacy of the form data and insert the user name, password and email entered by the user into the database table.
PHP code example:
<?php // 连接数据库 $connection = mysqli_connect("localhost", "root", "password", "database"); if (!$connection) { die("数据库连接失败:" . mysqli_connect_error()); } // 获取用户提交的表单数据 $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; // 验证表单数据的合法性 // ... // 使用预处理语句插入数据到数据库 $stmt = $connection->prepare("INSERT INTO user (username, password, email) VALUES (?, ?, ?)"); $stmt->bind_param("sss", $username, $password, $email); $stmt->execute(); // 关闭数据库连接 $stmt->close(); $connection->close(); ?>
To implement the user login function, you need to verify whether the user name and password entered by the user match those saved in the database data. If the match is successful, the user login is successful; otherwise, the login is considered failed.
PHP code example:
<?php // 连接数据库 $connection = mysqli_connect("localhost", "root", "password", "database"); if (!$connection) { die("数据库连接失败:" . mysqli_connect_error()); } // 获取用户提交的表单数据 $username = $_POST['username']; $password = $_POST['password']; // 使用预处理语句查询数据库 $stmt = $connection->prepare("SELECT * FROM user WHERE username = ?"); $stmt->bind_param("s", $username); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows == 1) { // 验证密码是否正确 $row = $result->fetch_assoc(); if (password_verify($password, $row['password'])) { // 登录成功 // ... } else { // 密码错误 // ... } } else { // 用户名不存在 // ... } // 关闭数据库连接 $stmt->close(); $connection->close(); ?>
Through the above code example, we can implement simple user registration and login functions. Of course, more security and user experience details may need to be considered in actual projects. I hope this article will be helpful to you in learning and practicing PHP development.
The above is the detailed content of PHP Development Guide: How to implement user registration and login functions. For more information, please follow other related articles on the PHP Chinese website!