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 tabels
1. temp_users
2. registered_users

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 sucessfull 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.

STEP1: 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 '',
PRIMARY KEY (`id`)
)

STEP2: join.php - Creating sign up form


<table width="350" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td><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>
<tr><td width="76">Name</td><td width="3">:</td>
<td width="305"><input name="name" type="text" id="name" size="30"></td>
</tr>
<tr><td>E-mail</td><td>:</td>
<td><input name="email" type="text" id="email" size="30"></td>
</tr>
<tr><td>password</td><td>:</td>
<td><input name="password" type="password" id="password" size="30"></td>
</tr>
<tr><td>Country</td><td>:</td>
<td><input name="country" type="text" id="country" size="30"></td>
</tr>
<tr><td>&nbsp;</td><td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit"> &nbsp;
<input type="reset" name="Reset" value="Reset"></td>
</tr></table></form></td></tr>
</table>

STEP3: 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.


<?php
include('settings.php');
$tbl_name=temp_users; 

// Generating random confirmation code 
$confirm_code=md5(uniqid(rand())); 

// 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')"; 

$result=mysql_query($sql);
// if data inserted suceesfully into database, send confirmation link to email

if($result){
  $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";
  $message.="http://www.yoursite.com/confirm.php?key=$confirm_code";     

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

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

// if your email succesfully sent 
if($sentmail){
 echo "Your Confirmation link Has Been Sent To Your Email Address."; 
}
 else{
 echo "Cannot send Confirmation link to your e-mail address"; 
}
?>

STEP4: 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"

<?php  
include('settings.php'); 

// Passkey that got from link 
$passkey=$_GET['key'];
$tbl_name1="temp_users";

// 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
if($result1){

//Counting how many row has this passkey
$count=mysql_num_rows($result1);

// 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'];
$tbl_name2="registered_users";

// 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')";
$result2=mysql_query($sql2);
}

// if passkey not found, display message "Wrong Confirmation code"
else{
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"

if($result2){
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'";
$result3=mysql_query($sql3);
 }
}
?>


settings.php – configuration of  your database


<?php

$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");
?>


Comments

Popular posts from this blog

How to construct a B+ tree with example

How to show only month and year fields in android Date-picker?

Visitor Counter Script Using PHP