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

How to remove whitespace from string using php

Remove All Special Characters From String PHP

Redirecting HTTP to HTTPS with PHP

Convert Base64 string to an image file

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.