PHP user registration email function to activate account

怪我咯
Release: 2023-03-10 14:52:01
Original
1305 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:


Account Activation Failed

"; 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 "

Account Activated

"; echo 'Login'; echo "
"; }else{ echo "

Account Activation Failed Or Already Activated

"; } ?>
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!