How to use PHP to write a login registration system with email verification function?
In the current Internet era, the login and registration system is one of the basic functions of various websites or applications. In order to improve security and user experience, users are often required to verify their email address when registering. This article will introduce how to use PHP to write a login registration system with email verification function.
1. Set up a development environment
First, we need to set up a PHP development environment. After installing PHP, you can choose to use a ready-made integrated development environment (such as XAMPP, WAMP, etc.) or configure Apache and MySQL yourself. Make sure the development environment is running properly.
2. Create a database
We need to create a MySQL database to store user information. You can use phpMyAdmin or the command line to create a database and create a data table named "users", including fields: id, username, email, password, is_verified.
Code example:
CREATE TABLE users (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
password VARCHAR(255) NOT NULL,
is_verified INT(1) DEFAULT 0
);
3. Write the registration page
Next, create a page named “register. php" file, used to implement the user registration function. The registration page contains a form that requires users to enter their username, email, and password, as well as a registration button. After the user clicks the registration button, the back-end code is executed to save the user information to the database and send a verification email to the user.
Code example:
//Connect to the database
$conn = new mysqli('localhost', 'root', '', 'database name' );
// Process the registration request submitted by the user
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username']; $email = $_POST['email']; $password = $_POST['password']; // 生成随机验证码 $verification_code = md5(uniqid(rand(), true)); // 插入用户数据到数据库 $query = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')"; $result = $conn->query($query); if ($result) { // 发送验证邮件 $subject = "邮箱验证"; $message = "点击链接验证您的邮箱:<a href='http://你的网站域名/verify.php?code=$verification_code'>点击此处</a>"; $headers = "From: your-email@gmail.com
";
$headers .= "Content-Type: text/html; charset=UTF-8
";
mail($email, $subject, $message, $headers); echo "注册成功,请查收邮件进行邮箱验证。"; } else { echo "注册失败,请重试。"; }
}
?>
4. Verification Email
We also need to create a file named "verify.php" to verify the user mailbox. When the user clicks on the verification link, the backend code is executed to update the verification status to Verified.
Code example:
//Connect to the database
$conn = new mysqli('localhost', 'root', '', 'database name' );
// Process email verification request
if (isset($_GET['code'])) {
$verification_code = $_GET['code']; // 更新验证状态 $query = "UPDATE users SET is_verified = 1 WHERE verification_code = '$verification_code'"; $result = $conn->query($query); if ($result) { echo "邮箱验证成功!"; } else { echo "邮箱验证失败,请重试。"; }
}
?>
5. Implement login
Finally, we create a file named "login.php" to implement the user login function. The login page contains a form that requires the user to enter their email and password, as well as a login button. After the user clicks the login button, the back-end code is executed to verify the user's email and password, and corresponding operations are performed based on the verification results.
Code example:
//Connect to the database
$conn = new mysqli('localhost', 'root', '', 'database name' );
// Process the login request submitted by the user
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST['email']; $password = $_POST['password']; // 查询用户信息 $query = "SELECT * FROM users WHERE email = '$email' AND password = '$password' AND is_verified = 1"; $result = $conn->query($query); if ($result->num_rows > 0) { // 登录成功 echo "登录成功!"; } else { echo "邮箱或密码错误,或邮箱未验证。"; }
}
?>