
PHP Registration with Email Verification using OTP
In this article, we will explain to you how to PHP registration with email verification using OTP. Sometimes when users register at a time send OTP in the mail. so we will see the example of how this can it possible in PHP.
Create database table in MySQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | CREATE TABLE `user` ( `id` int(11) NOT NULL, `first_name` varchar(255) NOT NULL, `last_name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `status` enum('0','1') NOT NULL, `otp` int(11) NOT NULL, `created_at` datetime DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- -- Indexes for dumped tables -- -- -- Indexes for table `user` -- ALTER TABLE `user` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `user` -- ALTER TABLE `user` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; COMMIT; |
Connect to Database
Create a new connection.php file. The code below is to provide how to connect with the database.
The following code is used to connect MySQL from PHP. It requires hostname, database username, database password, and database name.
connection.php
1 2 3 4 5 6 7 8 9 10 | <?php $hostname="localhost"; $username="root"; $password="test"; $database="testdb"; $conn = mysqli_connect($hostname,$username,$password,$database); ?> |
PHP Login Registration with Email Verification using OTP
Create a new PHP file register.php in this file in add the below code. in this example, we will create the user registration form using PHP. After the submit the register button then we will check all fields are mandatory or not. if all fields are not empty then we will store the data in the database and send OTP email.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | <?php if(isset($_POST['btnRegister'])) { include('connection.php'); $email = $_POST['email']; $sql=mysqli_query($conn,"SELECT * FROM user where email='$email'"); if(mysqli_num_rows($sql)>0) { $msg="Email Id Already Exists"; header ("Location: register.php?error=".$msg); exit; } else { $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; $password = md5($_POST['password']); $otp = rand(100000, 999999); $date = date("Y-m-d H:i:s"); if(!empty($first_name) && !empty($last_name) && !empty($email) && !empty($password)){ $insert="insert into user (first_name,last_name,email,password,status,otp,created_at) values('$first_name','$last_name','$email','$password',0,$otp,'$date')"; mysqli_query($conn,$insert); require 'class/class.phpmailer.php'; $mail = new PHPMailer; $mail->IsSMTP(); $mail->Host = 'ssl://smtp.gmail.com'; $mail->Port = '465'; $mail->SMTPAuth = true; $mail->Username = 'xxxxxxxxxxxx'; $mail->Password = 'xxxxxxxxxxx'; $mail->SMTPSecure = ''; $mail->From = 'Enter Form Email'; $mail->FromName = 'Enter From Name'; $mail->AddAddress($row['email']); $mail->IsHTML(true); $mail->Subject = 'User Registration with Email Verification using OTP'; $message_body = '<p>For verify your email address, enter this verification code when prompted:: <b>'.$otp.'</b>.</p> <p>Sincerely,</p>'; $mail->Body = $message_body; if($mail->Send()) { $code = base64_encode($email); header('location:verify.php?code='.$code); exit; } } else{ $msg="All fields are mandatory"; header ("Location: register.php?error=".$msg); exit; } } } ?> <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <title>PHP Registration with Email Verification using OTP</title> </head> <body> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container"> <a class="navbar-brand" href="register.php">Register</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> </div> </nav> <div class="container"> <div class="row"> <div class="col-12 col-sm-8 offset-sm-2 col-md-6 offset-md-3 mt-5 pt-3 pb-3 bg-white from-wrapper"> <div class="container"> <h3>Register</h3> <hr> <form class="" method="post"> <div class="row"> <div class="col-12 col-sm-6"> <div class="form-group"> <label for="firstname">First Name</label> <input type="text" class="form-control" name="firstname" id="firstname" value=""> </div> </div> <div class="col-12 col-sm-6"> <div class="form-group"> <label for="lastname">Last Name</label> <input type="text" class="form-control" name="lastname" id="lastname" value=""> </div> </div> <div class="col-12"> <div class="form-group"> <label for="email">Email address</label> <input type="text" class="form-control" name="email" id="email" value=""> </div> </div> <div class="col-12 col-sm-6"> <div class="form-group"> <label for="password">Password</label> <input type="password" class="form-control" name="password" id="password" value=""> </div> </div> <div class="col-12 col-sm-6"> <div class="form-group"> <label for="password_confirm">Confirm Password</label> <input type="password" class="form-control" name="password_confirm" id="password_confirm" value=""> </div> </div> <?php if (isset($_REQUEST['error'])): ?> <div class="col-12"> <div class="alert alert-danger" role="alert"> <?php echo $_REQUEST['error']; ?> </div> </div> <?php endif; ?> </div> <div class="row"> <div class="col-12 col-sm-4"> <button type="submit" name="btnRegister" class="btn btn-primary">Register</button> </div> </div> </form> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html> |
Email OTP Verification using PHP
verify.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | <?php if(isset($_POST['btnVerify'])) { include('connection.php'); $code = $_REQUEST['code']; if(!empty($code) && $_POST['otp'] != ""){ $sql=mysqli_query($conn,"SELECT * FROM user where email='".base64_decode($code)."' and status=0"); $row = mysqli_fetch_array($sql); if(is_array($row)) { if($_POST['otp'] == $row['otp']){ $msg="User Successfully verified"; header ("Location: verify.php?success=".$msg); exit; } else { $msg="Invalid OTP Code"; header ("Location: verify.php?code=".$code."&error=".$msg); exit; } } else { $msg="Invalid OTP Code"; header ("Location: verify.php?code=".$code."&error=".$msg); exit; } } else{ $msg="All fields are mandatory"; header ("Location: verify.php?code=".$code."&error=".$msg); exit; } } ?> <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <title></title> </head> <body> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container"> <a class="navbar-brand" href="verify.php">Verify</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> </div> </nav> <div class="container"> <div class="row"> <div class="col-12 col-sm-8 offset-sm-2 col-md-6 offset-md-3 mt-5 pt-3 pb-3 bg-white from-wrapper"> <div class="container"> <h3>User Verification using OTP</h3> <hr> <?php if (isset($_REQUEST['success'])): ?> <div class="alert alert-success" role="alert"> <?php echo $_REQUEST['success']; ?> </div> <?php endif; ?> <form class="" method="post"> <div class="form-group"> <label for="otp">Enter OTP</label> <input type="text" class="form-control" name="otp" id="otp" value=""> </div> <?php if (isset($_REQUEST['error'])): ?> <div class="col-12"> <div class="alert alert-danger" role="alert"> <?php echo $_REQUEST['error']; ?> </div> </div> <?php endif; ?> <div class="row"> <div class="col-12 col-sm-4"> <button type="submit" name="btnVerify" class="btn btn-primary">Verify</button> </div> <div class="col-12 col-sm-8 text-right"> <a href="resend.php">Resend</a> </div> </div> </form> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html> |
Resend Email using PHP
resend.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | <?php if(isset($_POST['btnResend'])) { include('connection.php'); $email = $_REQUEST['email']; if(!empty($email)){ $sql=mysqli_query($conn,"SELECT * FROM user where email='$email' and status=0"); $row = mysqli_fetch_array($sql); if(is_array($row)) { $otp = rand(100000, 999999); $update_query = "UPDATE user SET otp = '".$otp."' WHERE id = '".$row['id']."'"; mysqli_query($conn,$insert); require 'class/class.phpmailer.php'; $mail = new PHPMailer; $mail->IsSMTP(); $mail->Host = 'ssl://smtp.gmail.com'; $mail->Port = '465'; $mail->SMTPAuth = true; $mail->Username = 'xxxxxxxxxxxx'; $mail->Password = 'xxxxxxxxxxx'; $mail->SMTPSecure = ''; $mail->From = 'Enter Form Email'; $mail->FromName = 'Enter From Name'; $mail->AddAddress($row['email']); $mail->IsHTML(true); $mail->Subject = 'Resend Email Verification OTP For Registration'; $message_body = ' <p>For verify your email address, enter this verification code when prompted:: <b>'.$otp.'</b>.</p> <p>Sincerely,</p> '; $mail->Body = $message_body; if($mail->Send()) { $code = base64_encode($row['email']); header('location:verify.php?code='.$code); exit; } } else { $msg="Email Address already verified"; header ("Location: resend.php?error=".$msg); exit; } } else{ $msg="All fields are mandatory"; header ("Location: verify.php?code=".$code."&error=".$msg); exit; } } ?> <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <title>Resend Email Verification OTP using PHP</title> </head> <body> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container"> <a class="navbar-brand" href="resend.php">Resend</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> </div> </nav> <div class="container"> <div class="row"> <div class="col-12 col-sm-8 offset-sm-2 col-md-6 offset-md-3 mt-5 pt-3 pb-3 bg-white from-wrapper"> <div class="container"> <h3>Resend Email Verification OTP</h3> <hr> <?php if (isset($_REQUEST['success'])): ?> <div class="alert alert-success" role="alert"> <?php echo $_REQUEST['success']; ?> </div> <?php endif; ?> <form class="" method="post"> <div class="col-12"> <div class="form-group"> <label for="email">Email address</label> <input type="text" class="form-control" name="email" id="email" value=""> </div> </div> <?php if (isset($_REQUEST['error'])): ?> <div class="col-12"> <div class="alert alert-danger" role="alert"> <?php echo $_REQUEST['error']; ?> </div> </div> <?php endif; ?> <div class="row"> <div class="col-12 col-sm-4"> <button type="submit" name="btnResend" class="btn btn-primary">Resend</button> </div> </div> </form> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html> |