Email verification system in php

About Email Verification System

If you providing register and login feature on your website then email verification is very important whenever new user registered or signup on your website. Whenever a new user register on your website and you want to send verification code with link on his email id and if the user is genuine he clicks on link with code sent on his email id and then he verified himself for website. In this tutorial we are going to show you how to create a email verification php script.

In this tutorial we are using two mysql table for user verification one is “users_temp” and second is “users” table.
first table “users_temp” we are using for temporary user which are register but still not verify or he got a verification link but not click on that after click on link or verify user, user detail will be transfer in main table “users” it means you have only verify users in your primary table. so lets start.

Here we are explaining you step by step how to create email account verification script in php.

Step 1: Create “register.php” file For Create Account

1. Create a register.php file and create registration HTML form.
2. Create php script for user registration and insert data into a table.

<form name="signup" method="post">
    <input type="hidden" name="user_registeration" value="1" />
    <div class="form-group">
        <label>Full Name:</label>
        <input type="text" name="fullname" id="fullname" class="form-control" value="<?php if(isset($_POST['fullname']) && !empty($_POST['fullname'])){echo $_POST['fullname'];} ?>" />
    </div>
    
    <div class="form-group">
        <label>Email Address:</label>
        <input type="email" name="uemail" id="uemail" class="form-control" value="<?php if(isset($_POST['uemail']) && !empty($_POST['uemail'])){echo $_POST['uemail'];} ?>" />
    </div>
    
    <div class="form-group">
    <label>Password:</label>
        <input type="password" name="upassword" id="upassword" class="form-control" />
    </div>
    <input type="submit" name="signup" value="Sign Up" />
</form>

PHP code for user registration and send verification mail

Here i am inserting user detail into “users_temp” table and sending user name, password and verification email to user email id.

<?php
session_start();
$conn=mysqli_connect("localhost","root","") or die(mysqli_error($conn));
mysqli_select_db($conn, "chosenarticle") or die(mysqli_error($conn));
if (isset($_POST['user_registeration']))
{
    error_reporting( E_ALL );
    ini_set('display_errors', 1);
    $fullname = mysqli_real_escape_string($conn, $_POST['fullname']);
    $uemail = mysqli_real_escape_string($conn, $_POST['uemail']);
    $upassword = mysqli_real_escape_string($conn, $_POST['upassword']);
    $useridentity = mysqli_real_escape_string($conn, $_POST['useridentity']);
    $getEmail=mysqli_query($conn, "SELECT * FROM `users` WHERE `user_email`='$uemail'") or die(mysqli_error($conn));
    $TotEmail=mysqli_num_rows($getEmail);
    $flag=0;
    if(empty( $fullname ) || empty( $uemail ) || empty( $upassword ) || empty( $useridentity ))
    {
        $error='Required form field is missing';
        $flag=1;
    }
    if ( $TotEmail>0 )
    {
        $error='Entered email already exists!';
        $flag=1;
    }
    if ($flag==1)
    {
        $signUpError='<p style="color:#FF0000; text-aling:left;"><strong>ERROR</strong>: '.$error . '<br /></p>'; 
    }
    if ( $flag==0 )
    {
        $random_generate_pass_str = "abcdefghijklmnopqrstuwxyz0123456789";
        $tempPass = ""; //remember to declare $pass as an array
        $alphaLength = strlen($random_generate_pass_str) - 1; //put the length -1 in cache
        for ($i = 0; $i < 7; $i++)
        {
            $n = rand(0, $alphaLength);
            $tempPass.= $random_generate_pass_str[$n];
        }
        
        $varification_code=md5($tempPass);
        $md5pass=md5($upassword);
        mysqli_query($conn, "insert into `users_temp` (`user_email`, `password`, `full_name`, `type`, `varification_code`) values('$uemail', '$md5pass', '$fullname', '$useridentity', '$varification_code')") or die(mysqli_error($conn));
        $query = mysqli_query($conn, "SELECT id from users_temp WHERE `user_email` = '$uemail' AND varification_code='$varification_code'") or die(mysqli_error($conn));
        $result = mysqli_fetch_assoc($query);
        $lastid = $result['id'];
        
        $varification_url=$baseurl."verify.php?nonid=".$lastid."&norplyemail=".$uemail."&verifycode=".$varification_code;
        
        $to=$uemail;
        $subject="Account Created Successfully";
        $headers = 'Content-Type: text/html; charset=UTF-8';
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "From: PHPKIDA Team <info@phpkida.com>\r\n";
        //$headers .= 'Cc:test@wordpress.org, test@wordpress.org';
        $message="<strong>Dear ".$fullname."</strong><br>Thank you for registering with Chosen Articles.<br>Below are your login credentials:<br><strong>Email-Id: ".$uemail."<br>Password: ".$upassword."</strong><br><br>Click this link to active your account:<br>".$varification_url."<br><br><br>Best Regards,<br>PHPKIDA Team";
        
        mail( $to, $subject, $message, $headers );
        $SuccMsg=1;
    }
}
?>

Email verification page “verify.php”

Whenever user click on verification link then this “verify.php” will be execute, and check called verify link is valid or not, this user already verify or not, this link has been expire or not and then if called url is valid then user will be verify and user detail insert into “users” table.

<?php
if (isset($_REQUEST['nonid']) && isset($_REQUEST['norplyemail']) && isset($_REQUEST['verifycode']) && !empty($_REQUEST['nonid']) && !empty($_REQUEST['norplyemail']) && !empty($_REQUEST['verifycode']))
{
    $nonid = $_REQUEST['nonid'];
    $norplyemail = $_REQUEST['norplyemail'];
    $verifycode = $_REQUEST['verifycode'];
    
    $GetAru=mysqli_query($conn, "SELECT * FROM users WHERE `user_email` = '$norplyemail'");
    $GetAruNum=mysqli_num_rows($GetAru);
    
    if($GetAruNum<=0)
    {
        $query = mysqli_query($conn, "select * from users_temp where `user_email` = '$norplyemail' AND `id` ='$nonid' AND varification_code='$verifycode'");
        $result = mysqli_fetch_assoc($query);
        $TotalRow=mysqli_num_rows($query);
        if($TotalRow>0)
        {
            $email=$result['user_email'];
            $password=$result['password'];
            $fullname=$result['full_name'];
            $type=$result['type'];
            
            $insert=mysqli_query($conn, "insert into users(user_email, password, full_name, type, status, varify) values('$email', '$password', '$fullname', '$type', '1', '1')");
            $Msg="<p class='success_register'>Your account has been activated, you can now login.</p>";
        }
        else
        {
            $Msg="<p class='error'>This link has been expired.</p>";
        }
    }
    else
    {
        $Msg="<p class='error'>Your account already Active.</p>";
    }
}
else
{
    $Msg="<p class='error'>Invalid approach, please use the link that has been sent to your email.</p>";
}
?>

Leave a Reply

Related Posts: You may like

Extract Total Number of Image And Missing Alt Attribute using php

How to extract Heading tags using PHP from string

How to remove whitespace from string using php

Remove All Special Characters From String PHP

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.