How to Account Verification System Through Email Using PHP

So the flow is quite simple, user registers to a site, and email is sent to their email address for verification. User then click a link in the email message which redirect him back to the site where it’s account gets approved.

HTML Code

<!DOCTYPE html>
<html>
<head></head>
<body>
    <form method="post" action="submit.php">
        <input type="email" name="email" required="">
        <input type="password" name="password" required="">
        <input type="submit" name="register" Value="Register">
    </form>
</body>
</html>

PHP Code

if(isset($_POST['register']))
{
    $email_id=  $_POST['email'];
    $pass=  $_POST['password'];
    $code=substr(md5(mt_rand()),0,15);

    $insert = mysqli_query($conn,"insert into verify(email,password,code)values($email','$pass','$code')");
    $db_id = $conn->insert_id;

    $message = "Your Activation Code is ".$code."";
    $to = $email;
    $subject = "Activation Code For scripts.guru";
    $from = 'demo@gmail.com';
    $body='Your Activation Code is '.$code.' Please Click On This link submit.php?id='.$db_id.'&code='.$code.'to activate your account.';
    $headers = "From:".$from;
    mail($to,$subject,$body,$headers);
    echo "Email Send please verify your account";
}

Click on Email Link

if(isset($_GET['id']) && isset($_GET['code']))
{
    $id=$_GET['id'];
    $code=$_GET['id'];
    $select=mysqli_query($conn,"select email,password from verify where id='$id' and code='$code'");
    if(mysqli_num_rows($select) > 0)
    {
        $row=mysql_fetch_array($select);
        $email=$row['email'];
        $password=$row['password'];
        $insert_user = mysqli_query($conn,"insert into verified_user (email,password) values('$email','$password')");
        $delete      = mysqli_query($conn,"delete from verify where id='$id' and code='$code'");
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *