PHP Register with Email Verification Example
Apr 23, 2022 . Admin

Hello dev,
Here, I will show you PHP Register with Email Verification Example. I would like to show you User Registration with Email Verification in PHP. you will learn PHP & MySQL. we will help you to give an example of PHP Email Verification For New User Accounts .
This article will give you a simple example of User registration and email verification PHP and MySQL. We will use the simple example of creating PHP and MySQL User Registration with Email Verification.
I will give you a simple example of How to Implement Email Verification for New Members.
So, let's see bellow solution:
connection.php<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "aatman"; $conn = new mysqli($servername,$username,$password,$dbname); if($conn->connect_error){ die ('connection faild:'.$conn->connect_error); } ?>create table
CREATE TABLE user ( fullName VARCHAR(50) NOT NULL, username VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(50) NOT NULL, verification_id VARCHAR(255) NOT NULL, verification_status INT(3) NOT NULL, );index.php
<?php session_start(); require ('connection.php'); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>PHP Register with Email Verification Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </head> <body class="bg-dark"> <div class="container-fluid mt-3"> <div class="card" style="height:590px;"> <div class="card-header text-center"> <h1>PHP Register with Email Verification Example</h1> </div> <div class="card-body"> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand " href="#">Aatmaninfo</a> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav me-auto mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">about us</a> </li> <li class="nav-item"> <a class="nav-link" href="#">contect us</a> </li> </ul> </div> <form class="justify-content-end"> <?php if (isset($_SESSION['logged_in']) && $_SESSION['logged_in']==TRUE) { echo $_SESSION['email']." - <a href='logout.php' class='btn btn-danger'>LOGOUT</a>"; }else{ echo "<button type='button' class='btn btn-success m-1' data-bs-toggle ='modal' data-bs-target='#loginModal'>Login</button> <button type='button' class='btn btn-danger m-1' data-bs-toggle='modal' data-bs-target='#RegisterModal'>Register</button>"; } ?> </form> </nav> <?php if (isset($_SESSION['logged_in']) && $_SESSION['logged_in']==TRUE) { echo "<h1 class='text-center mt-5 pt-5'>Welcom to this website</h1>"; } ?> </div> </div> <div class="modal fade" id="loginModal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h3 class="modal-title" id="loginModalLabel">Login</h3> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <form action="registration.php" method="post"> <div class="modal-body"> <div class="mb-3"> <label>Email : </label> <input type="text" name="email_username" class="form-control" placeholder="Email"> </div> <div class="mb-3"> <label>Password : </label> <input type="password" name="password" class="form-control" placeholder="Password"> </div> </div> <div class="modal-footer"> <input type="submit" name="login" class="btn btn-primary"> <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button> </div> </form> </div> </div> </div> <div class="modal fade" id="RegisterModal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h3 class="modal-title" id="RegisterModalLabel">Register</h3> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <form action="registration.php" method="post"> <div class="modal-body"> <div class="mb-3"> <label>Full Name : </label> <input type="text" name="fullName" class="form-control" placeholder="Full Name"> </div> <div class="mb-3"> <label>User Name : </label> <input type="text" name="username" class="form-control" placeholder="User Name"> </div> <div class="mb-3"> <label>Email : </label> <input type="email" name="email" class="form-control" placeholder="Email"> </div> <div class="mb-3"> <label>Password : </label> <input type="password" name="password" class="form-control" placeholder="Password"> </div> </div> <div class="modal-footer"> <input type="submit" name="register" class="btn btn-primary"> <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button> </div> </form> </div> </div> </div> </div> </body> </html>
download this file : PHPMailer
registration.php<?php require ('connection.php'); session_start(); use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; function sendmail($email,$v_cod){ require ('PHPMailer-master/src/PHPMailer.php'); require ('PHPMailer-master/src/Exception.php'); require ('PHPMailer-master/src/SMTP.php'); $mail = new PHPMailer(true); try { $mail->SMTPDebug = SMTP::DEBUG_SERVER; $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'your email @gmail.com'; $mail->Password = 'your password'; $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->Port = 465; $mail->setFrom('your email @gmail.com', 'sender name'); $mail->addAddress($email); $mail->isHTML(true); $mail->Subject = 'email verification from aatmaninfo'; $mail->Body = "Thanks for registration.<br>click the link bellow to verify the email address <a href='http://localhost:8000/post-email/verify.php?email=$email&v_cod=$v_cod'>verify</a>"; $mail->send(); return true; } catch (Exception $e) { return false; } } if (isset($_POST['login'])) { $email_username =$_POST['email_username']; $password_login =$_POST['password']; $sql="SELECT * FROM user WHERE email = '$email_username' AND password = '$password_login' AND verification_status = '1'"; $result = $conn->query($sql); if ($row = $result->fetch_assoc()) { $_SESSION['logged_in']=TRUE; $_SESSION['email']=$row['email']; header('location:index.php'); }else{ echo " <script> alert('please verify your email!!'); window.location.href='index.php' </script>"; } } if (isset($_POST['register'])) { $fullName =$_POST['fullName']; $username =$_POST['username']; $email =$_POST['email']; $password =$_POST['password']; $user_exist_query="SELECT * FROM user WHERE username= '$username' AND email = '$email' "; $result = $conn->query($user_exist_query); if ($result) { if ($result->num_rows > 0) { $row = $result->fetch_assoc(); if ($row['username'] === $username && $row['email'] === $email) { echo " <script> alert('username alredy taken!'); window.location.href='index.php' </script>"; }else{ echo " <script> alert('email alredy register'); window.location.href='index.php' </script>"; } }else{ $v_cod=bin2hex(random_bytes(16)); $query ="INSERT INTO `user`(`fullName`, `username`, `email`, `password`,`verification_id`, `verification_status`) VALUES ('$fullName','$username','$email','$password','$v_cod','0')"; if (($conn->query($query)===TRUE) && sendmail($email,$v_cod )===TRUE) { echo " <script> alert('register successful.chack your mailbox in inbox or spam and verify your account.'); window.location.href='index.php' </script>"; }else{ echo " <script> alert('query can not run'); window.location.href='index.php' </script>"; } } }else{ echo " <script> alert('query can not run'); window.location.href='index.php' </script>"; } } ?>verify.php
<?php require ('connection.php'); if (isset($_GET['email']) && isset($_GET['v_cod'])) { $email = $_GET['email']; $v_cod = $_GET['v_cod']; $sql="SELECT * FROM user WHERE email = '$email' AND verification_id = '$v_cod'"; $result = $conn->query($sql); if ($result) { if ($result->num_rows == 1) { $row = $result->fetch_assoc(); $fetch_Email = $row['email']; if ($row['verification_status'] == 0) { $update = "UPDATE user SET verification_status='1' WHERE email = '$fetch_Email'"; if ($conn->query($update)===TRUE) { echo " <script> alert('verification successful'); window.location.href='index.php' </script>"; }else{ echo " <script> alert('query can not run'); window.location.href='index.php' </script>"; } }else{ echo " <script> alert('email alredy register'); window.location.href='index.php' </script>"; } } } }else{ echo " <script> alert('server down!!'); window.location.href='index.php' </script>"; } ?>logout.php
<?php session_start(); session_unset(); session_destroy(); header("location:index.php"); ?>Output: Registration Page


Login page
.png)
Home page
.png)
It will help you...