Email verification or confirmation with php and mysqli

If you want to create a Email verification script on your web page by which user can verify their email during registration page or any other pages.You can create a simple email verification script using php and mysql.

Required:

  • Create a Table for users
  • Create a Register/Login form code
  • Sending Verification Email

Let’s do that –

Suppose uers table will have 4 columns –

  • email
  • password
  • act_status
  • act_token

The column “act_token” will store the unique activation token(used to activate the account) of the user and “act_status” will store the account status. If it is 0 the account is not activated and if it is one the account is activated and user can login,”act_status” will have a  default value of  0.

Create a Register/Login form code

<html>
<head><title>Register OR Login</title>
<link rel=”stylesheet” href=”style.css” />
</head>
<body>
<form action=”register.php” method=”post”>
<table>
<tbody ><tr><td colspan=”2″><h1>Register OR Login</h1></td><tr>
<tr><td>Email : </td><td><input name=”email” type=”email” placeholder=”Your Email” /></td></tr>
<tr><td>Password : </td><td><input name=”password” type=”password” placeholder=”Your New Password” /></td></tr>
<tr><td><input name=”register” type=”submit” value=”Register” /></td>
<td><span>OR</span><input type=”submit” name=”login” value=”Login” /></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

After entering the email and password if user clicks register he will be registered and if login is clicked and the account is created and activated  he will login. A unique activation token will also be stored in the database along with the other information when user will register. This token will be random so user must have to go and click the activation link to enable his account.

Below are some CSS styles for the form.

* { 
font-family:sans-serif;
color:green;
}
input {
border:1px solid green;
border-radius:5px;
padding:2px;
margin:10px;
color:gray;
}
 {
border-radius:5px;
border:1px solid green;
margin:auto;
}
h1 {
color:white;
background:green;
font-weight:lighter;
border-radius:5px;
}
tr{
text-align:center;
}
input[type='submit']{
font-weight:bolder;
}
input[type='submit']:hover {
cursor:pointer;
background:white;
}

Sending Verification Email

Now we need to create script for sending  the activation email to the user so that user can proceed with the account verification.The verification email will be sent during the registration process. The activation link in the email will also contain the activation token of the user. Below is the code to login and register:

<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
$con = null;
if(isset($_POST['register']) && isset($_POST['']) && isset($_POST['password']))
{
$act_token = bin2hex(openssl_random_pseudo_bytes(32));
$con = new i("localhost" , "root" , "root" , "test_db");
if($con != null)
{	//Register code
$query = "insert into users (,password,act_token) " . 
" values('" . $_POST[''] . "' , '" . $_POST['password'] . "' , '" . $act_token ."');";
if($con->query($query)){
// if you put it in the root of your website $act_url will be like "http://yourdomain/Activate.php?actToken=".$act_token;
$act_url = "http://localhost/php/-verification/Activate.php?actToken=" . $act_token;
sendMail('<'.$_POST[''].'>' , $act_url);
}
else echo '<span style="color:red;">Some Error Occured!</span>';
$con->close();
}
}
else if(isset($_POST['login']) && isset($_POST['']) && isset($_POST['password']))
{   //Login code
$con = new i("localhost" , "databaseuser" , "databesepassword" , "databaseNAME");
//let the user login if username and password are correct and account is activated i.e act_status=1
$query = "select * from users where ='".$_POST['']."' and password='".$_POST['password']."' and act_status='1';";
if($con != null)
$result = $con->query($query);
if($result->num_rows > 0)
echo '<span style="color:green;">Login Successfull!</span>' ;
else	
echo '<span style="color:red;">Users With specified details do not Exist Or the Account is not Verified!!</span>';
}
function sendMail($to , $act_link){
$to = '<' . $_POST[''] . '>';
$from = '<admin@yourdomain.com>';
$subject = 'Activation ';
$HTMLbody = "<body>This an activation  from phpfragments click ".$act_link." to activate Your Account</body>";
//Send an  to the user with the activation link
$headers['From'] = $from;
$headers['To'] = $to;
$headers['Subject'] = $subject;
$mime = new Mail_mime();
$mime->setHTMLBody($HTMLbody);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', array(
'host' => 'mail.yourdomain.com', // if using gmail then host will be smtp.gmail.com
'port' => '110',//if GMAIL port will be 465
'auth' => true ,
'username' => 'admin@yourdomain.com' ,//if gmail your gmail username i.e daniequreshi@gmail.com 
'password' => 'yourpassword'));//if gmail your gmail password
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
}
else echo "<span style='color:green;'>Registered. Activation  Sent</span>";
}
?>

In the code above first we have included the PEAR mail API(included in the downloadable source) files which we will use to send the email to user. Then in the if condition we have checked if the register button is clicked and email,password are provided(isset$_POST[’email’],isset$_POST[‘password’])  if yes we will create a unique activation token on line 7 , then we will connect to database(line 8) and save the user’s information along with the token in the database. And after saving user in the database we will send an html email to the user with the activation link on line 17 the sendMail function contains simple script for sending an email in html format.

Else if the login button was clicked and email and password are entered. Check if a user with the same email and password exists in the database and if that account is activated(act_status=’1′) if both of  these are true show a message saying “login Successful” and if not show an error message. Below is the the simple script for account activation.

<?php
if(isset($_GET['actToken']))
{
$con = new i("localhost" , "databaseuers" , "databasepassword" , "databasename");
$query = "update users set act_status='1' where act_token='". $_GET['actToken'] ."';";
if($con && $con->query($query))
//if you have uploaded LoginORRegister.php on your domain's root than link will be "http://yourdomain/LoginORRegister.php"
echo '<span style="color:green;">Account Activated You can login now
<a href="http://localhost/php/-verification/LoginORRegister.php">Login</a></span>';
$con->close();
}
?>

When the user clicks the verification link in the email a GET request is made to “activate.php” with the “actToken” as a parameter. In the above code if the URL contains  the “actToken” the account status of the account with that token is activated by setting the act_status to ‘1’.

Download the source code by clicking the button below.

[button color=”blue” size=”medium” link=”http://divson.in/wp-content/uploads/2017/01/email-verification.rar” icon=”downlaod” target=”false”]DOWNLOAD EMAIL CONFIRMATION[/button]



Author: Gautam Jha
I am Gautam Jha from New Delhi, India. I love to write code and blogs, traveling, Video games, sometimes study as well.

Leave a Reply

Hey there!

Sign in

Forgot password?
Close
of

Processing files…