PHP user registration email activation account implementation code, php user registration

Source: Internet
Author: User
Tags php example

PHP user registration email activation account implementation code, php user registration

When registering an account for a website, we usually receive an email containing a click link to activate it. How does it activate our account? Today, I would like to introduce you to a method.

Prerequisites

When registering an account, we generally record our id, username, password, email address, or mobile phone number in the User table. There is also an Account Activation field. Let's assume it isactivation varchar(50) An activation code is usually generated during registration, and the activation code is inserted into the activation field. We can use$activation_key=bin2hex(openssl_random_pseudo_bytes(16)); And insert it into the field.

Save activation information in the Link

Since we have activated the account after clicking the link in the email, it means that the activation information is stored in this link. What information should it store? First, there must be an activation code and a user's email address or id at the time of registration. Here we use an email address. For example, the following link.

Copy codeThe Code is as follows:
$ Message. = "<a href =". "rel =" external nofollow "http://www.XXXXX.com/activate.php? Email = ". urlencode ($ email)." & key = $ activation_key "."> Activate </a> ";

$emailLet us use the email address when we registerurlencode()Encode the string for the request part of the URL,$activation_keyThis is the generated activation code. We use the server to send an activation email to this email, which is displayed as a hyperlink in the mailbox client, prompting you to click. After clicking, we will send the email and key to activate. PHP file, that is, start to verify and activate the account.

Verify information in activate. php

We use a URL request to send a request to activate. php imports the user email address and activation code. Now, you only need to retrieve the original information in the database for comparison to verify the user. So we inserted the activation code into the user table in the first step. I wrote an activate. php example:

<? Php $ link = mysqli_connect ("localhost", "root", "root", "project") or die ("Database Access Denied"); // connect to the Database if (! Isset ($ _ GET ['email ']) |! Isset ($ _ GET ["key"]) {echo "<div class = 'alert alert-danger '> Account Activation Failed </div>"; exit; // If the email link is damaged and no user activation information is input, no subsequent steps are performed.} $ email = $ _ GET ['email ']; $ key = $ _ GET ['key']; $ email = mysqli_real_escape_string ($ link, $ email); $ key = mysqli_real_escape_string ($ link, $ key ); // escape special characters $ SQL = "UPDATE users SET activation = 'activated' WHERE (user_email = '$ email' AND activation = '$ key') LIMIT 1 "; $ result = mysqli_query ($ link , $ SQL); // query the data items matching the email and key, and update the activation field to activatedif (mysqli_affected_rows ($ link) = 1) {echo "<div> Account Activated </div>"; echo '<a href = "index. php "rel =" external nofollow "type =" button "> Login </a> '; echo" <br/> ";} else {echo "<div> Account Activation Failed Or Already Activated </div>" ;}?>

Now the account is activated. When you log on, first match whether activation is activated, and then proceed to the subsequent steps.

This article only involves the solution to this problem. A complete user registration, activation, and logon should also be configured with the server that sends the email.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.