All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Account Verification System Through Email Using PHP

Last Updated : Jul 1, 2023

IN - PHP HTML | Written & Updated By - Amruta

In this tutorial we will create and Account Verification System through Email using PHP, account verification is very important whenever new user registered or signup on your website. Whenever a new user register a verification code with link is sent on his email id.

If the user is genuine he clicks on link with code sent on his email id and then he verified himself for website. You may also like Send Email Using PHP

Account Verification System Through Email Using PHP

To Create Account Verification System it takes only Two steps:-

  1. Make a HTML file and define markup and script for Registration form
  2. Make a PHP file to insert data and email the code

Step 1. Make a HTML file and define markup and script for Registration form

We make a HTML file and save it with a name registration.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>

 <form method="post" action="verification.php">
  <input type="text" name="email">
  <input type="password" name="password">
  <input type="submit" name="register" Value="Register">
 </form>

</body>
</html>

In this step we make a registration form that sends all the user data to verification.php page

Step 2. Make a PHP file to insert data and email the code

We make a PHP file named verification.php

<?php

// Table Scheme for Verify Table
CREATE TABLE `verify` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `email` text NOT NULL,
 `password` text NOT NULL,
 `code` text NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1

// Table Scheme for verified_user table
CREATE TABLE `verified_user` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `email` text NOT NULL,
 `password` text NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1

if(isset($_POST['register']))
{
	$email_id=$_POST['email'];
	$pass=$_POST['password'];
	$code=substr(md5(mt_rand()),0,15);
	mysql_connect('localhost','root','');
	mysql_select_db('sample');
	
	$insert=mysql_query("insert into verify values('','$email','$pass','$code')");
	$db_id=mysql_insert_id();

	$message = "Your Activation Code is ".$code."";
    $to=$email;
    $subject="Activation Code For Talkerscode.com";
    $from = 'your email';
    $body='Your Activation Code is '.$code.' Please Click On This link <a href="verification.php">Verify.php?id='.$db_id.'&code='.$code.'</a>to activate your account.';
    $headers = "From:".$from;
    mail($to,$subject,$body,$headers);
	
	echo "An Activation Code Is Sent To You Check You Emails";
}

if(isset($_GET['id']) && isset($_GET['code']))
{
	$id=$_GET['id'];
	$code=$_GET['id'];
	mysql_connect('localhost','root','');
	mysql_select_db('sample');
	$select=mysql_query("select email,password from verify where id='$id' and code='$code'");
	if(mysql_num_rows($select)==1)
	{
		while($row=mysql_fetch_array($select))
		{
			$email=$row['email'];
			$password=$row['password'];
		}
		$insert_user=mysql_query("insert into verified_user values('','$email','$password')");
		$delete=mysql_query("delete from verify where id='$id' and code='$code'");
	}
}

?>

In this step we make two isset() conditions to insert and verify the user.In first condition we get all user submitted data and insert in our verify table which is used as temporary user table and then we send the code with activation link to the user email id which he enters in registration form we use simple mail() function to send email you can also use SMTP to send email.

In second condition we get the id and code value from the link when user clicks on link send to his email id and and then get all the user data from verify table and insert in verified_user table and then delete that user from verify table beacuse user verified his account.You may also like validate email and password using jQuery.

Remember always validate data before inserting in database if you want to know to how to validate you can refer our tutorial How to do validation before and after submitting the form

Thats all, this is how to Create Account Verification System Through Email Using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on php account verification helps you and the steps and method mentioned above are easy to follow and implement.