Email Verification on User Registration In PHP

Hello guys, this post is about email verification system for new user registration. You would have seen 99.9% of websites and web applications want you to verify your email upon registration. Only users with verified email id’s will be able to login. Its really very important to verify email id’s before any user can login into your system. There are various ways to verify users email address, I will demonstrate one of them.Its very simple to implement. There are various test cases that should be kept in mind when creating a email verification system for new user registration. I have tried to cover all the scenarios, If you find any case which I have missed then do let me know I will surely Improve the script. Below is a simple flowchart:

email verification flowchart

LIVE DEMO   DOWNLOAD

I have created one table “REGISTER” to store user’s name, email and password. This table also contains two more columns “IsActive” and “Hash”. IsActive can have two values 1 or 0 depending on email id is verified or not respectively. Hash field stores MD5 hash which is sent to user’s email for verification.

Register Table Schema

I have created “register.php” which is responsible for entering new users information in REGISTER table and send email to user’s given email id for verification. As soon as user clicks the ACTIVATE BUTTON in his email, “verify.php” does its job by activating this email and now user can login into our application.

Code for register.php:

if(isset($_POST['submitform'])){

$name = trim(mysql_escape_string($_POST['name']));
$email = trim(mysql_escape_string($_POST['email']));
$passwords = trim(mysql_escape_string($_POST['pwd']));
$password = md5($passwords);

$query_verify_email = "SELECT * FROM register WHERE email ='$email' and isactive = 1";
$verified_email = mysqli_query($con,$query_verify_email);
if (!$verified_email) {
echo ' System Error';
}
if (mysqli_num_rows($verified_email) == 0) {
// Generate a unique code:
$hash = md5(uniqid(rand(), true));
$query_create_user = "INSERT INTO `register` ( `name`, `email`, `password`, `hash`) VALUES ( '$name', '$email', '$password', '$hash')";
$created_user = mysqli_query($con,$query_create_user);
if (!$created_user) {
echo 'Query Failed ';
}

if (mysqli_affected_rows($con) == 1) { //If the Insert Query was successfull.

$subject = 'Activate Your Email';

$headers = "From: admin@infotuts.com \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$url= BASE_PATH . '/verify.php?email=' . urlencode($email) . "&key=$hash";

$message ='<p>To activate your account please click on Activate buttton</p>';
$message.='<table cellspacing="0" cellpadding="0"> <tr>';
$message .= '<td align="center" width="300" height="40" bgcolor="#000091" style="-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px;

color: #ffffff; display: block;">';

$message .= '<a href="'.$url.'" style="color: #ffffff; font-size:16px; font-weight: bold; font-family: Helvetica, Arial, sans-serif; text-decoration: none;

line-height:40px; width:100%; display:inline-block">Click to Activate</a>';
$message .= '</td> </tr> </table>';

mail($email, $subject, $message, $headers);

echo '<div>A confirmation email
has been sent to <b>'. $email.' </b> Please click on the Activate Button to Activate your account </div>';
} else { // If it did not run OK.
echo '<div>You could not be registered due to a system
error. We apologize for any
inconvenience.</div>';
}
}
else{
echo '<div>Email already registered</div>';}
}

?>

Code For Verfify.php:

<?php
$email = trim(mysql_escape_string($_GET['email']));

$key = trim(mysql_escape_string($_GET['key']));

$query_verify_email = "SELECT * FROM register WHERE email ='$email' and isactive = 1";
$result_verify_email = mysqli_query($con,$query_verify_email);

if (mysqli_num_rows($result_verify_email) == 1)
{
echo '<div>Your Account already exists. Please <a href="login.html">Login Here</a></div>';

}
else
{

if (isset($email) && isset($key))

{

mysqli_query($con, "UPDATE register SET isactive=1 WHERE email ='".$email."' AND hash='".$key."' ") or die(mysql_error());

if (mysqli_affected_rows($con) == 1)
{
echo '<div>Your Account has been activated. Please <a href="login.html">Login Here</a></div>';

} else
{
echo '<div>Account couldnot be activated.</div>';

}
}
mysqli_close($con);

}?>

You can Download the file and try it on your server.
NOTE: This will not work in localserver because localserver doesn’t have mail server configured by default. So if you want to test it in localserver then I would suggest you to use any email server package like PHPMailer or sendmail. Here’s an article explaining how to send email from local server.

Share this tutorial with your friends and followers 🙂

48 thoughts on “Email Verification on User Registration In PHP”

  1. sir,
    I would like, how to create a list of mails,and at time to sending information(ex: temple design or plain-text) receive that all mails.

    How to create a mailchimp campaigns. please kindly i request to you post the information to that link.

        1. thanks, i already can access the file but i get another problem when login system, its always say: Incorrect Credentials, You need to register

          how can i solve that problem, please help me 🙁

        2. Hello InfoTuts, same problem above. my email is already registered, but i can’t download your code.

          any help from you?
          Thanks in Advance

  2. thanks it very nice. but i want added code for after we click the activation verification link it automatically verified and redirect the user dasboard or user main page not again login page.. please help me thanks in advance

  3. Hello Sir I want to download this feature for my site. But this feature does not download. Plz tell me how can i download this feature.

  4. Your “Email Verification on User Registration In PHP” cannot be downloaded after subscribe. Same problems as others. How to download? thanks

  5. dr sir, i have already subscribe on this website but i still cant download the source file.?? can you plz send me the file..thanks in advance

  6. sir, i am so sorry but i can’t download the file even i had been subscribing….
    please send me now the link or the file. Thank you

      1. Parse error: syntax error, unexpected ‘;’ in C:\xampp\htdocs\email-verification-php\register.php on line 11. What’s up with that? I added and deleted the bracket but still i got that error.

  7. Dear Sir
    i’m already subcribed but there are some problem for downloading..
    pls help me..
    I want to learn, Is it working?

  8. Parse error: syntax error, unexpected ‘;’ in C:\wamp\www\email-verification-php\register.php on line 11

    how to fix this?

  9. Hi,
    Im using your code to send email and its working perfectly. The issue I have is that Im using MVC Framework. How do I set the URL using the email and key to verify the user (www.example.com/controller/method)

  10. Pingback: Regsitration Activation Link |

Comments are closed.