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

[button_round color=”blue” url=”infotuts.com/demo/email-verification-php/”] LIVE DEMO [/button_round] [button_round color=”blue” url=”infotuts.com/demo/email-verification-php/download.html”] DOWNLOAD [/button_round]

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 🙂


Posted

in

, ,

by

Comments

48 responses to “Email Verification on User Registration In PHP”

  1. chintu Avatar

    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.

  2. khilendra Avatar
    khilendra

    i’m already subcribed but there are some problem for downloading..
    pls help me..

    1. InfoTuts Avatar
      InfoTuts

      Hi,

      Please get your code now.

      Thanks:
      Sanjeev

    2. azmi Avatar

      me too 🙁

      1. InfoTuts Avatar
        InfoTuts

        Hi,
        Email list has been updated. Please get the code now.

        Thanks:
        Sanjeev

        1. azmi Avatar

          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. JhomZ Avatar
          JhomZ

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

          any help from you?
          Thanks in Advance

          1. InfoTuts Avatar
            InfoTuts

            Hi,

            Please download your file now.

            Thanks:
            Sanjeev

  3. Aradhya Avatar
    Aradhya

    Cannot download code even after subscribing, Pls help!!!

  4. zubair Avatar
    zubair

    i’m already subcribed but there are some problem for downloading..
    pls help me..

    1. InfoTuts Avatar
      InfoTuts

      Please download your file.

  5. name Avatar
    name

    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

  6. Beuth_10 Avatar
    Beuth_10

    i’m already subcribed but there are some problem for downloading..
    pls help me..

    1. InfoTuts Avatar
      InfoTuts

      Done please download the file.

  7. hamood alazri Avatar

    still we have a problem in downloading could you send the link to my email directly
    alazri11@hotmail.com

  8. aiwendil Avatar
    aiwendil

    Thank you for your tutorial but I cannot download the file.

    Help me, please.

  9. Rajesh Avatar
    Rajesh

    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.

    1. InfoTuts Avatar
      InfoTuts

      Hi, Please subscribe and download the file.

  10. Rezaul Avatar
    Rezaul

    Hi
    I need to download the file. I am subscribed. Please help me out.

    Rezaul

    1. InfoTuts Avatar
      InfoTuts

      Please get your file now.

      Thanks:
      InfoTuts Team

  11. brian cherdak Avatar

    unable to download file, can you please send?

  12. john Avatar
    john

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

  13. yadevi Avatar
    yadevi

    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

  14. Eron Avatar

    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

  15. alson Avatar
    alson

    Already subscribed but still cannot download file, pls help. thank you.

  16. mikky Avatar
    mikky

    Subscribed but no download how is dis?

  17. wissam Avatar
    wissam

    I already registered but i can’t download your code, please help 🙂

  18. Mark Loring Avatar

    Modified for my web site. Works great! Thank You so much.

  19. tara khati Avatar
    tara khati

    i already subscribe but cant download the code.please help.

  20. kunal Avatar
    kunal

    I already registered but i can’t download your code, please help.

  21. ade Avatar
    ade

    i’m already subcribed but there are some problem for downloading..
    pls help me with the download

  22. mich Avatar
    mich

    Im already registered but i can’t download your code, please help. Thanks..

  23. samin Avatar
    samin

    I can’t download the file even though I have already subscribed. Please send the link or the file directly to me at saminbhp@gmail.com

    1. InfoTuts Avatar
      InfoTuts

      Hi,

      Did you get the code? If not then you can download it now.

      Thanks:
      Sanjeev

      1. samin Avatar
        samin

        Thank you very much!

      2. samin Avatar
        samin

        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.

  24. John Avatar
    John

    Already subscribe too. But can’t download it.

    1. InfoTuts Avatar
      InfoTuts

      Please download now.

  25. romel Avatar
    romel

    unable to download file, can you send it, please!

    1. InfoTuts Avatar
      InfoTuts

      Hi.

      Please get your file now 🙂

  26. ning Avatar
    ning

    Already subscribe. But can’t download it.

  27. Amos Avatar
    Amos

    I have subscribed but I’m unable to download the code. Please help

    1. InfoTuts Avatar
      InfoTuts

      Please download the file now.

  28. hamdi Avatar
    hamdi

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

    1. InfoTuts Avatar
      InfoTuts

      Hi,

      Please download your file now.

      Thanks:
      Sanjeev

  29. Armel Avatar
    Armel

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

    how to fix this?

  30. Sanaa Khan Avatar
    Sanaa Khan

    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)

  31. […] Registration Activation […]