Through this tutorial, you will learn how to send a user confirmation email with an activation link and code in PHP and MySQL.
How to Create Login & Signup Form with Email Verification using PHP and MySQL?
Here are steps:
- Step 1: Create Table In DB by Sql Query
- Step 2: Create a Database Connection PHP File
- Step 3: Create User Registration Page PHP
- Step 4: Store User Registration Data and Send Email
- Step 5: Create Email Verification PHP File
- Step 6 – Create Login Form In PHP with MySQL
- Step 7- Create User Profile and Fetch data From Database
- Step 8 – Create Logout.php file
Step 1: Create Table In DB by Sql Query
First of all, open your phpmyadmin and run the following sql query. To create table into your selected database:
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `email` varchar(100) NOT NULL, `mobile` varchar(100) NOT NULL, `password` varchar(250) NOT NULL, `status` int(11) NOT NULL DEFAULT '0', `email_verification_link` varchar(255) NOT NULL, `email_verified_at` TIMESTAMP NULL, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Step 2: Create a Database Connection PHP File
In this step, create a file name db.php and add the following code into db.php file:
<?php $servername='localhost'; $username='root'; $password=''; $dbname = "my_db"; $conn=mysqli_connect($servername,$username,$password, $dbname); if(!$conn){ die('Could not Connect MySql Server:' .mysql_error()); } ?>
Note that, This code is used to create a MySQL database connection in PHP project.
Step 3: Create User Registration Page PHP
In this step, create a user registration php file that named index.php and add the below PHP and HTML code into index.php file.
Note that, This HTML code shows the user registration form.
Now, add the following html form into index.php file:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>User Registration with Email Verification in PHP</title> <!-- CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container mt-5"> <div class="card"> <div class="card-header text-center"> User Registration with Email Verification in PHP </div> <div class="card-body"> <form action="store-registration-send-email.php" method="post"> <div class="form-group"> <label for="exampleInputEmail1">Name</label> <input type="text" name="name" class="form-control" id="name" required=""> </div> <div class="form-group"> <label for="exampleInputEmail1">Email address</label> <input type="email" name="email" class="form-control" id="email" aria-describedby="emailHelp" required=""> <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small> </div> <div class="form-group"> <label for="exampleInputEmail1">Password</label> <input type="password" name="password" class="form-control" id="password" required=""> </div> <div class="form-group"> <label for="exampleInputEmail1">Confirm Password</label> <input type="password" name="cpassword" class="form-control" id="cpassword" required=""> </div> <input type="submit" name="password-reset-token" class="btn btn-primary"> </form> </div> </div> </div> </body> </html>
Step 4: Store User Registration Data and Send Email
In this step, create new PHP file named store-registration-send-email.php. This file PHP code will store user registration data into db table. And As well as send an email verification link to user and store it into MySQL DB.
To add the following php code into store-registration-send-email.php file:
<?php if(isset($_POST['password-reset-token']) && $_POST['email']) { include "db.php"; $result = mysqli_query($conn,"SELECT * FROM users WHERE email='" . $_POST['email'] . "'"); $row= mysqli_num_rows($result); if($row < 1) { $token = md5($_POST['email']).rand(10,9999); mysqli_query($conn, "INSERT INTO users(name, email, email_verification_link ,password) VALUES('" . $_POST['name'] . "', '" . $_POST['email'] . "', '" . $token . "', '" . md5($_POST['password']) . "')"); $link = "<a href='localhost/email-verification/verify-email.php?key=".$_POST['email']."&token=".$token."'>Click and Verify Email</a>"; require_once('phpmail/PHPMailerAutoload.php'); $mail = new PHPMailer(); $mail->CharSet = "utf-8"; $mail->IsSMTP(); // enable SMTP authentication $mail->SMTPAuth = true; // GMAIL username $mail->Username = "[email protected]"; // GMAIL password $mail->Password = "your_gmail_password"; $mail->SMTPSecure = "ssl"; // sets GMAIL as the SMTP server $mail->Host = "smtp.gmail.com"; // set the SMTP port for the GMAIL server $mail->Port = "465"; $mail->From='[email protected]'; $mail->FromName='your_name'; $mail->AddAddress('reciever_email_id', 'reciever_name'); $mail->Subject = 'Reset Password'; $mail->IsHTML(true); $mail->Body = 'Click On This Link to Verify Email '.$link.''; if($mail->Send()) { echo "Check Your Email box and Click on the email verification link."; } else { echo "Mail Error - >".$mail->ErrorInfo; } } else { echo "You have already registered with us. Check Your email box and verify email."; } } ?>
Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.
Once less secure apps are enabled; now you can use your Gmail for sending the emails.
Step 5: Create Email Verification PHP File
In this step, create email verification php file that named verify-email.php. This php file code verify user registration email from mysql database.
So, add the following php and html code into verify-email.php file:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>User Account Activation by Email Verification using PHP</title> <!-- CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <?php if($_GET['key'] && $_GET['token']) { include "db.php"; $email = $_GET['key']; $token = $_GET['token']; $query = mysqli_query($conn, "SELECT * FROM `users` WHERE `email_verification_link`='".$token."' and `email`='".$email."';" ); $d = date('Y-m-d H:i:s'); if (mysqli_num_rows($query) > 0) { $row= mysqli_fetch_array($query); if($row['email_verified_at'] == NULL){ mysqli_query($conn,"UPDATE users set email_verified_at ='" . $d . "' WHERE email='" . $email . "'"); $msg = "Congratulations! Your email has been verified."; }else{ $msg = "You have already verified your account with us"; } } else { $msg = "This email has been not registered with us"; } } else { $msg = "Danger! Your something goes to wrong."; } ?> <div class="container mt-3"> <div class="card"> <div class="card-header text-center"> User Account Activation by Email Verification using PHP </div> <div class="card-body"> <p><?php echo $msg; ?></p><br> <a href="login.php" class="btn btn-default">Login</a> </div> </div> </div> </body> </html>
Step 6 – Create Login Form In PHP with MySQL
Create login form, where you accept user email id and password and authenticate users from database. So you can create a login.php file and add the below code into your file:
<?php session_start(); require_once "db.php"; if(isset($_SESSION['user_id'])!="") { header("Location: dashboard.php"); } if (isset($_POST['login'])) { $email = mysqli_real_escape_string($conn, $_POST['email']); $password = mysqli_real_escape_string($conn, $_POST['password']); if(!filter_var($email,FILTER_VALIDATE_EMAIL)) { $email_error = "Please Enter Valid Email ID"; } if(strlen($password) < 6) { $password_error = "Password must be minimum of 6 characters"; } $result = mysqli_query($conn, "SELECT * FROM users WHERE email = '" . $email. "' and pass = '" . md5($password). "'"); if(!empty($result)){ if ($row = mysqli_fetch_array($result)) { $_SESSION['user_id'] = $row['uid']; $_SESSION['user_name'] = $row['name']; $_SESSION['user_email'] = $row['email']; $_SESSION['user_mobile'] = $row['mobile']; header("Location: dashboard.php"); } }else { $error_message = "Incorrect Email or Password!!!"; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Simple Login Form in PHP with Validation | Tutsmake.com</title> <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <div class="row"> <div class="col-lg-10"> <div class="page-header"> <h2>Login Form in PHP with Validation</h2> </div> <p>Please fill all fields in the form</p> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <div class="form-group "> <label>Email</label> <input type="email" name="email" class="form-control" value="" maxlength="30" required=""> <span class="text-danger"><?php if (isset($email_error)) echo $email_error; ?></span> </div> <div class="form-group"> <label>Password</label> <input type="password" name="password" class="form-control" value="" maxlength="8" required=""> <span class="text-danger"><?php if (isset($password_error)) echo $password_error; ?></span> </div> <input type="submit" class="btn btn-primary" name="login" value="submit"> <br> You don't have account?<a href="registration.php" class="mt-3">Click Here</a> </form> </div> </div> </div> </body> </html>
Step 7- Create User Profile and Fetch data From Database
Create a new file name dashboard.php and add the below code into your file.
The below code used to show logged in user data.
<?php session_start(); if(isset($_SESSION['user_login_id']) =="") { header("Location: login.php"); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>User Info Dashboard | Tutsmake.com</title> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row"> <div class="col-lg-8"> <div class="card"> <div class="card-body"> <h5 class="card-title">Name :- <?php echo $_SESSION['user_name']?></h5> <p class="card-text">Email :- <?php echo $_SESSION['user_email']?></p> <p class="card-text">Mobile :- <?php echo $_SESSION['user_mobile']?></p> <a href="logout.php" class="btn btn-primary">Logout</a> </div> </div> </div> </div> </div> </body> </html>
Step 8 – Create Logout.php file
Create logout.php file and update the below code into your file.
The below code is used to destroy login your data and logout.
<?php ob_start(); session_start(); if(isset($_SESSION['user_login_id'])) { session_destroy(); unset($_SESSION['user_id']); unset($_SESSION['user_name']); unset($_SESSION['user_email']); unset($_SESSION['user_mobile']); header("Location: login.php"); } else { header("Location: login.php"); } ?>
Conclusion
Login and Registration form with email verification using PHP MySQL tutorial, you have learned how to send activation/verfication link after user login and registration in PHP and MySQL.
Recommended PHP Tutorials
If you want to download the user registration with email verification in php source code. So click here to download this project.
it works for me
Great work 👌👌
simply amazing, Devendra full stack developer
Many thanks for this. I found it very helpful.