How to use PHP to implement email verification login registration function?

王林
Release: 2023-08-18 09:08:01
Original
1405 people have browsed it

How to use PHP to implement email verification login registration function?

How to use PHP to implement email verification login registration function?

With the development of the Internet, the login and registration function plays a vital role in websites and applications. In order to protect users' account security and information security, many websites and applications have introduced email verification mechanisms. This article will introduce how to use PHP to implement the login and registration function of email verification, and provide corresponding code examples.

  1. Create database and table structure

First, we need to create a database and create a user table in it to store registered user information. You can use the following SQL statement to create a table structure:

CREATE DATABASE `userdb`;
USE `userdb`;

CREATE TABLE `users` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(50) NOT NULL,
  `email` VARCHAR(50) NOT NULL,
  `password` VARCHAR(255) NOT NULL,
  `is_verified` INT(1) NOT NULL DEFAULT '0',
  `verification_code` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login
  1. Code implementation of registration function

First, we need to create a registration page (register.php), user Enter your email and password to register. After the user clicks the registration button, we need to generate a random verification code and send it to the user's email. You can use the following code example:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    $verification_code = md5(uniqid(rand(), true));

    // 将用户信息插入数据库
    $conn = new mysqli('localhost', 'username', 'password', 'userdb');

    $sql = "INSERT INTO users (name, email, password, verification_code) VALUES ('$name', '$email', '$password', '$verification_code')";
    
    if ($conn->query($sql) === TRUE) {
        // 发送验证邮件
        $subject = "请验证您的邮箱";
        $message = "请点击以下链接完成验证:
";
        $message .= "http://yourdomain.com/verify.php?code=$verification_code";
        $headers = "From: noreply@yourdomain.com";

        mail($email, $subject, $message, $headers);
        echo "注册成功,请前往您的邮箱验证账户!";
    } else {
        echo "注册失败,请稍后再试!";
    }

    $conn->close();
}
?>
Copy after login
  1. Code implementation of email verification function

After the user clicks the verification link in the registration email, we need to verify the verification code validity, and updates the is_verified field in the user table. You can use the following code example:

<?php
if (isset($_GET['code']) && !empty($_GET['code'])) {
    $verification_code = $_GET['code'];

    $conn = new mysqli('localhost', 'username', 'password', 'userdb');

    $sql = "SELECT id FROM users WHERE verification_code='$verification_code' AND is_verified=0";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        $sql = "UPDATE users SET is_verified=1 WHERE verification_code='$verification_code'";

        if ($conn->query($sql) === TRUE) {
            echo "验证成功!";
        } else {
            echo "验证失败,请稍后再试!";
        }
    } else {
        echo "无效的验证码!";
    }

    $conn->close();
}
?>
Copy after login
  1. Code implementation of the login function

The login function is relatively simple. After the user enters the correct email and password, we need to verify the user input Whether the email address and password match, and the email address has passed email verification. You can use the following code example:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = $_POST['email'];
    $password = $_POST['password'];

    $conn = new mysqli('localhost', 'username', 'password', 'userdb');

    $sql = "SELECT id FROM users WHERE email='$email' AND password='$password' AND is_verified=1";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo "登录成功!";
    } else {
        echo "登录失败,请检查邮箱和密码是否正确,或者邮箱是否已验证!";
    }

    $conn->close();
}
?>
Copy after login

Through the above code example, we can implement the basic email verification login registration function. Of course, in practical applications, other security measures need to be added, such as password encryption, blocking multiple incorrect logins, etc.

I hope this article is helpful to you, and I wish you happy development!

The above is the detailed content of How to use PHP to implement email verification login registration function?. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!