PHP user registration email function to activate account
When we register an account on a website, we usually receive an email containing a click link to activate the account. This article mainly introduces the implementation code of PHP email activation account. Those who are interested can learn more
When we register an account on a website, we usually receive an email containing a click link to activate our account. How does it activate our account? Today I will introduce a method to you.
Preparatory conditions
When we register an account, we usually record our ID, user name, password, email or mobile phone number in the user table, and also There is a field for whether the account is activated or not. We assume it is activation varchar(50)
When registering, an activation code is generally generated, and the activation code is inserted into the activation field. We can use $activation_key=bin2hex(openssl_random_pseudo_bytes(16));
to generate and insert it into the field.
Save activation information in the link
Since we activated the account after clicking the link in the email, it means that the activation information must be stored in this link . What information should it store? First of all, it must have an activation code and the user's email or ID when registering. We use email here. For example, this link below.
Copy code The code is as follows:
$message.="Activate";
$email
Let’s use urlencode()
to encode the email we used when registering, and use the string for the request part of the URL, $activation_key
is the activation code we generated. We use the server to send the activation email to this email, and this will be displayed as a hyperlink in the email client, prompting you to click. After clicking, we will send the email and key to activate.php file, which starts to perform verification and activate the account.
Verify the information in activate.php
We use the URL request to pass in the user email and activation code to activate.php. Now we only need to retrieve the original information in the database By comparing the information, we can verify the user, so our first step is to insert the activation code into the user table. I wrote an example of activate.php:
<?php $link=mysqli_connect("localhost", "root", "root", "project") or die("Database Access Denied");//连接数据库 if (!isset($_GET['email'])||!isset($_GET["key"])){ echo "<p class='alert alert-danger'>Account Activation Failed</p>"; exit;//如果邮件链接损坏,没有传入用户激活信息,则不执行后续步骤 } $email=$_GET['email']; $key=$_GET['key']; $email=mysqli_real_escape_string($link, $email); $key=mysqli_real_escape_string($link, $key);//转义特殊字符 $sql="UPDATE users SET activation='activated' WHERE (user_email='$email' AND activation='$key') LIMIT 1"; $result=mysqli_query($link, $sql);//查询email与key匹配的数据项,并且更新activation字段到activated if (mysqli_affected_rows($link)==1){ echo "<p>Account Activated</p>"; echo '<a href="index.php" rel="external nofollow" type="button">Login</a>'; echo "<br/>"; }else{ echo "<p>Account Activation Failed Or Already Activated</p>"; } ?>
Now the account is activated. When the user logs in, he first checks whether the activation is activated, and then proceeds with the subsequent steps.
This article only covers the solution to this problem. A complete user registration, activation, login, and a server for sending emails should also be configured
The above is the detailed content of PHP user registration email function to activate account. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
