When we register an account of a website, we usually receive an email with a click link activation, this article mainly describes the PHP email activation Account Implementation code, interested in can understand
When we sign up for an account on a website, we usually receive an email with a click link activation, and how does it activate our account, and today we'll introduce you to a method.
Pre-conditions
When we register an account, we generally record our ID, username, password, email or mobile number in the user's table, and there will be a field where the account is activated or not. We assume that it activation varchar(50)
will normally generate an activation code for registration, and insert the activation code into the activation field, which we can use $activation_key=bin2hex(openssl_random_pseudo_bytes(16));
to generate and insert it into the field.
Save activation information in a link
Now that we have clicked on the link in the email and activated the account, the activation information must be stored in this link. And what information it should store, first of all, there must be an activation code, as well as the user registration of the mailbox or ID, we use the mailbox here. For example, the following link.
Copy the Code code as follows:
$message. = "<a href=". "Rel=" external nofollow "http://www.XXXXX.com/activate.php?email=". UrlEncode ($email). " &key= $activation _key "." >Activate</a> ";
$email
So that we register the mailbox, we use urlencode()
to encode, the string used for the URL of the request part, $activation_key
is the activation code we generated, we use the server to send an activation email to this email, and this in the mailbox client, will be displayed as a hyperlink, prompting you to click, click, We sent the email and key to the activate.php file, which is the beginning of the verification and activation of the account.
Validating information in activate.php
We use the URL request to activate.php incoming user mailbox and activation code, now only need to tune the database of the original information to compare, we can verify the user, so we first step to insert the activation code in the user table, I wrote a activate.php example:
<?php$link=mysqli_connect ("localhost", "root", "root", "project") or Die ("Database Access Denied");//Connect to the DB if (! Isset ($_get[' email ') | |! Isset ($_get["key"]) { echo "<p class= ' alert Alert-danger ' >account Activation failed</p>"; exit;//If the message link is corrupted and there is no incoming user activation information, do not follow the steps} $email =$_get[' email '); $key =$_get[' key '; $email =mysqli_real_escape_string ($ Link, $email), $key =mysqli_real_escape_string ($link, $key);//Escape special character $sql= "UPDATE users SET activation= ' activated ' WHERE (user_email= ' $email ' and activation= ' $key ') LIMIT 1 "; $result =mysqli_query ($link, $sql);//Query the data item that the email matches the key, and update the Activation field to Activatedif (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, the user at the login, first match the activation is activated, followed by the next steps
This article only addresses the solution to this problem, a full user registration, activation, login, and should also configure the server to send mail