Home >Backend Development >PHP Tutorial >PHP programming tutorial: How to use the membership system
PHP Programming Tutorial: How to use the membership system
1. Introduction
The membership system is one of the commonly used functions in modern website development, which can realize user registration, Login, password retrieval, personal information management and other functions. This article will introduce how to design a simple membership system using the PHP programming language and provide corresponding code examples.
2. Preparation work
Before starting to write the membership system, we need to prepare the following environments and tools:
3. Create a database
First, we need to create a database to store the data required by the membership system. MySQL or other relational databases can be used. In MySQL, you can use the following code to create a database named "member_system":
CREATE DATABASE member_system;
Then, create a data table named "users" to store user information:
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL );
4. Registration function
Next, let’s implement the registration function. The user fills in the username, password and email address on the registration page and submits the form. We need to save user submitted data into the database.
First, create a file named "register.php" to handle the registration form submitted by the user. In this file, we need to connect to the database and insert user information into the "users" table. The following is a sample code:
<?php // 连接数据库 $conn = new mysqli('localhost', 'root', 'password', 'member_system'); if ($conn->connect_error) { die("连接失败:" . $conn->connect_error); } // 处理注册表单提交 if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; $created_at = date('Y-m-d H:i:s'); // 验证用户名是否已存在 $sql = "SELECT * FROM users WHERE username='$username'"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "用户名已存在,请重新选择!"; } else { // 将用户信息插入数据库 $sql = "INSERT INTO users (username, password, email, created_at) VALUES ('$username', '$password', '$email', '$created_at')"; if ($conn->query($sql) === TRUE) { echo "注册成功!"; } else { echo "注册失败:" . $conn->error; } } $conn->close(); } ?>
In the above code, we use the mysqli object to connect to the database and obtain the data submitted by the user through the $_POST variable. Then, use SQL statements to insert the user information into the database.
Next, create a file called "register.html" to display the registration form. The following is a sample code:
<!DOCTYPE html> <html> <head> <title>注册</title> </head> <body> <h2>注册账号</h2> <form method="post" action="register.php"> <input type="text" name="username" placeholder="用户名" required><br> <input type="password" name="password" placeholder="密码" required><br> <input type="email" name="email" placeholder="邮箱" required><br> <input type="submit" value="注册"> </form> </body> </html>
Users can see the registration form when they visit the "register.html" page in their browser. After filling out the form and submitting it, it will jump to the "register.php" page and display the corresponding prompt information.
5. Login function
Next, we will implement the login function. The user enters the username and password on the login page and can log in to the system after passing the verification.
First, create a file named "login.php" to handle the login form submitted by the user. In this file, we need to connect to the database and query whether the username and password entered by the user match the data in the database. The following is a sample code:
<?php // 连接数据库 $conn = new mysqli('localhost', 'root', 'password', 'member_system'); if ($conn->connect_error) { die("连接失败:" . $conn->connect_error); } // 处理登录表单提交 if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = $_POST['username']; $password = $_POST['password']; // 验证用户名和密码是否匹配 $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "登录成功!"; } else { echo "登录失败,请检查用户名和密码!"; } $conn->close(); } ?>
In the above code, we use the mysqli object to connect to the database and obtain the user name and password entered by the user through the $_POST variable. Then, use SQL statements to query the corresponding data in the database.
Next, create a file named "login.html" to display the login form. The following is a sample code:
<!DOCTYPE html> <html> <head> <title>登录</title> </head> <body> <h2>用户登录</h2> <form method="post" action="login.php"> <input type="text" name="username" placeholder="用户名" required><br> <input type="password" name="password" placeholder="密码" required><br> <input type="submit" value="登录"> </form> </body> </html>
Users can see the login form when they visit the "login.html" page in the browser. After filling in the form and submitting it, it will jump to the "login.php" page and display the corresponding prompt information.
6. Improved functions
In addition to registration and login functions, the membership system can also provide other functions, such as password retrieval, personal information management, etc. Here, I will use the above code as a basis and leave it to the readers to further improve and expand.
7. Summary
Through the introduction and code examples of this article, we have learned how to use the PHP programming language to design a simple membership system to implement registration and login functions. Hope this helps readers who are new to PHP programming. In actual development, the membership system can be further improved and expanded according to needs. I wish you happy programming and continuous progress!
The above is the detailed content of PHP programming tutorial: How to use the membership system. For more information, please follow other related articles on the PHP Chinese website!