Account verification using PHP

If you haven't read my previous article then, please Read This. It will be very helpful.So many times you may have experienced this , When you sign up to join any website, they may want to verify your email address by sending confirmation link to your email address. You'll learn how this thing works in this tutorial.

In this tutorial, we are going to create  4 php files
1. join.php
2. join_ac.php
3. confirm.php
4. settings.php

And 2 tables
1. temp_users
2. registered_users

Script Logic

1. When users sign up. We will create a random set of confirmation code.
2. Keeping the details and confirmation code in table "temp_users ". This is temporary table, we have to move this details  to table "registered_users" after email address has been verified.
3. After a successful insertion of details into table "temp_users", send confirmation link to email that users used to sign up, if email is invalid they will not receive our email.
4. They have to click on confirmation link to activate their account.

1. Creating table "temp_users" & "registered_users"

Table "temp_users"

CREATE TABLE `temp_users` (
`confirm_code` varchar(65) NOT NULL default '',
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`password` varchar(15) NOT NULL default '',
`country` varchar(65) NOT NULL default ''

Table "registered_users"
CREATE TABLE `registered_users` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`password` varchar(65) NOT NULL default '',
`country` varchar(65) NOT NULL default '',

2. join.php - Creating sign up form

<table width="350" border="0" align="center" cellpadding="0" cellspacing="0">
 <form name="form1" method="post" action="join_ac.php">
  <table width="100%" border="0" cellspacing="4" cellpadding="0">
   <tr><td colspan="3"><strong>Sign up</strong></td></tr>
              <td width="76">Name</td><td width="3">:</td>
       <td width="305"><input name="name" type="text" id="name" size="30"></td>
       <td><input name="email" type="text" id="email" size="30"></td>
       <td><input name="password" type="password" id="password" size="30"></td>
       <td><input name="country" type="text" id="country" size="30"></td>
       <td><input type="submit" name="Submit" value="Submit"> &nbsp;
       <input type="reset" name="Reset" value="Reset"></td>

3. join_ac.php - Inserting data into database

1. Generating random confirmation code.
2. Inserting data and confirmation code into database.
3. Sending email to user with confirmation link.


// Generating random confirmation code 

// Receiving values from post data
$name = $_POST['name']; 
$email = $_POST['email'];
$country = $_POST['country'];

// Inserting data into database
$sql="INSERT INTO $tbl_name(confirm_code, name, email, password, country)VALUES('$confirm_code', '$name', '$email', '$password', '$country')"; 

// if data inserted suceesfully into database, send confirmation link to email

  $subject="Your confirmation link here";
  $header="from: your name <your email>";
  $message="Your Comfirmation link \r\n";
  $message.="Click on this link to activate your account \r\n";

// sending email 
$sentmail = mail($to,$subject,$message,$header); 

// if not found 
 echo "Sorry..! We can’t find your email in our database";

// if your email succesfully sent 
 echo "Your Confirmation link Has Been Sent To Your Email Address."; 
 echo "Cannot send Confirmation link to your e-mail address"; 

4. confirm.php

When your user open his email he'll see this message and link to file "confirm.php" including key in url.

1. Checking  key from the URL.
2. If key  found in database,then  move all data in that row from table "temp_users" to "registered_users".
3. Delete key from table "temp_users"


/* Passkey that got from link */

/* Retrieve data from table where row that match this passkey*/
$sql1="SELECT * FROM $tbl_name1 WHERE confirm_code ='$passkey'"; $result1=mysql_query($sql1);

/* If successfully queried*/

/*Counting how many row has this passkey*/

/* if found this passkey in our database, retrieve data from table "temp_users"*/ if($count==1){
$rows = mysql_fetch_array($result1);
$name = $rows['name'];
$email= $rows['email'];
$password = $rows['password'];
$country= $rows['country'];

/* Inserting data that retrieves from "temp_users" into table "registered_users"*/
$sql2="INSERT INTO $tbl_name2(name, email, password, country)VALUES('$name', '$email', '$password', '$country')";

/* if passkey not found, display message "Wrong Confirmation code"*/
echo "Wrong Confirmation code";

/* if the data successfully moved from table"temp_users" to table
"registered_users" then it will displays message "Your account has been
activated".Now we have to  delete confirmation code from table "temp_users"*/

echo "Your account has been activated";
/* Delete information of this user from table "temp_users" that has this passkey*/
$sql3="DELETE FROM $tbl_name1 WHERE confirm_code = '$passkey'";

settings.php – configuration of  your database


$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="";// Database name 

//Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");