Home Backend Development PHP Tutorial PHP user registration email function to activate account

PHP user registration email function to activate account

Jun 16, 2017 am 11:11 AM
E-mail verification

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[&#39;email&#39;])||!isset($_GET["key"])){
  echo "<p class=&#39;alert alert-danger&#39;>Account Activation Failed</p>";
  exit;//如果邮件链接损坏,没有传入用户激活信息,则不执行后续步骤
}
$email=$_GET[&#39;email&#39;];
$key=$_GET[&#39;key&#39;];
$email=mysqli_real_escape_string($link, $email);
$key=mysqli_real_escape_string($link, $key);//转义特殊字符
$sql="UPDATE users SET activation=&#39;activated&#39; WHERE (user_email=&#39;$email&#39; AND activation=&#39;$key&#39;) LIMIT 1";
$result=mysqli_query($link, $sql);//查询email与key匹配的数据项,并且更新activation字段到activated
if (mysqli_affected_rows($link)==1){
  echo "<p>Account Activated</p>";
  echo &#39;<a href="index.php" rel="external nofollow" type="button">Login</a>&#39;;
  echo "<br/>";
}else{
  echo "<p>Account Activation Failed Or Already Activated</p>";
}
?>
Copy after login


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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles