PHP user registration email function to activate account

怪我咯
Release: 2023-03-10 14:52:01
Original
1445 people have browsed it

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!

Related labels:
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!