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!