Confirmation Mail for User Registration Using PHP

By | November 5, 2012

In user registration, user have to provide their email ID. We don’t know user provided email ID is original or fake, Here is the tutorial for confirmation mail for user registration using php .we have to verify whether the email is belong to the user or some unknown person. After submitting  values in user registration form, a confirmation mail will be sent to the email, which is provided by the user. In confirmation mail, a link to our website with some values will be sent.

Link will have following

  • primary key value( Eg: id )
  • confirmation code

Need of primary key value is, by using primary key value we have fetch confirmation code from database and validate with confirmation code in mail’s link. If confirmation code in database and confirmation code in mail’s link are same, then user will be verified ( update database field verified as 1).

Database Fields

id – primary key field
firstname 
lastname
password
email
confirm_id – rand number generated and stored
verified – ‘0’ is for unverified and ‘1’ is for verified user

Confirmation mail for user registration
config.php

[code type=php]
$connect=mysql_connect(“localhost”,”root”,””);
mysql_select_db(“user”,$connect);
[/code]

registration.php

In this page user registration will happen and confirmation will be sent to the user’s email.

[code type=php]

<html>
<body>
<?php
if(isset($_POST[‘submit’]))
{
$fname=mysql_real_escape_string($_POST[‘fname’]);
$lname=mysql_real_escape_string($_POST[‘lname’]);
$pass=mysql_real_escape_string($_POST[‘pass’]);
$email=mysql_real_escape_string($_POST[‘mail’]);
$rand=rand(100000,100000000);
$query2=mysql_query(“insert into user values(”,’$fname’,’$lname’,’$pass’,’$email’,’$rand’,’0′)”);
if($query2)
{
student_confirmation($id,$fname,$lname,$rand,$email);
}
}

function student_confirmation($id,$fname,$lname,$rand,$email)
{
$subject = “Email Verification mail”;
$headers = “From: email@domain.com \r\n”;
$headers .= “Reply-To: email@domain.com \r\n”;
$headers .= “MIME-Version: 1.0\r\n”;
$headers .= “Content-Type: text/html; charset=ISO-8859-1\r\n”;

$message = ‘<html><body>’;
$message.='<div style=”width:550px; background-color:#CC6600; padding:15px; font-weight:bold;”>’;
$message.=’Email Verification mail’;
$message.='</div>’;
$message.='<div style=”font-family: Arial;”>Confiramtion mail have been sent to your email id<br/>’;
$message.=’click on the below link in your verification mail id to verify your account ‘;
$message.=”<a href=’http://yourdomain.com/user-confirmation.php?id=$id&email=$email&confirmation_code=$rand’>click</a>”;
$message.='</div>’;
$message.='</body></html>’;

mail($email,$subject,$message,$headers);
}
?>

<form method=”post” action=””>
First Name: <input type=”text” name=”fname”>
Last Name: <input type=”text” name=”lname”>
password: <input type=”text” name=”pass”>
Email: <input type=”text” name=”mail”>
<input type=”submit” name=”submit”>
</form>

</body>
</html>
[/code]

 

User Link will be validation here.

user-confirmation.php

In this page user email has to be verified. When user click on the verification link in this page only the verification link will be validated.
[code type=php]
<html>
<body>
<?php
if(isset($_GET[‘id’]) && isset($_GET[‘confirmation_code’]) && isset($_GET[’email’]))
{
$id=$_GET[‘id’];
$code=$_GET[‘confirmation_code’];
$email=$_GET[’email’];
$query=mysql_query(“select * from user where id=’$id’ AND email=’$email’ AND confirm_id=’$code’ “);
$row=mysql_num_rows($query);
if($row == 1)
{
$query1=mysql_query(“update user set verified=’1′ where id=’$id’ AND  email=’$email’ AND confirm_id=’$code'”);
if($query1)
{
echo “You have verified your mail ID”;
}
}
}
?>
</body>
</html>
[/code]

6 thoughts on “Confirmation Mail for User Registration Using PHP

  1. leen

    hey, i am newbie and also like ur post, but i want to ask, why the config.php does not connect between regirstration.php and user-confirmation.php?I hope ur reply, thanks^^

    Reply

Leave a Reply to leen Cancel reply

Your email address will not be published. Required fields are marked *

coemption-unvitalized