How to use PHP to write a login registration system with email verification function?

王林
Release: 2023-08-19 13:58:02
Original
1035 people have browsed it

How to use PHP to write a login registration system with email verification function?

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
Copy after login

";

    $headers .= "Content-Type: text/html; charset=UTF-8
Copy after login

";

    mail($email, $subject, $message, $headers);

    echo "注册成功,请查收邮件进行邮箱验证。";
} else {
    echo "注册失败,请重试。";
}
Copy after login

}
?>

">

<label for="username">用户名:</label>
<input type="text" name="username" id="username" required><br>

<label for="email">邮箱:</label>
<input type="email" name="email" id="email" required><br>

<label for="password">密码:</label>
<input type="password" name="password" id="password" required><br>

<input type="submit" value="注册">
Copy after login

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 "邮箱验证失败,请重试。";
}
Copy after login

}
?>

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 "邮箱或密码错误,或邮箱未验证。";
}
Copy after login

}
?>

">

<label for="email">邮箱:</label>
<input type="email" name="email" id="email" required><br>

<label for="password">密码:</label>
<input type="password" name="password" id="password" required><br>

<input type="submit" value="登录">
Copy after login

Through the above steps, we have implemented a login registration system with email verification function. Users need to verify their email address when registering, which improves security and provides a better user experience.

The above is the detailed content of How to use PHP to write a login registration system with email verification 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