Tuesday, April 8, 2014

Email Verification using PHP

Hi friends.In this article I will explain about Email verification using PHP

Let's take a look.

Create table using below sql commands:

CREATE TABLE IF NOT EXISTS `register` (
  `UserId` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(100) NOT NULL,
  `Email` varchar(100) NOT NULL,
  `Username` varchar(100) NOT NULL,
  `Password` varchar(100) NOT NULL,
  `activation` varchar(100) NOT NULL,
  PRIMARY KEY (`UserId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;




Database config file:

Conn.php

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db_name = "email"; //replace with your database name
//database connection
$link = mysql_connect($host, $user, $pass);
$db=mysql_select_db($db_name);

?>

Index.php

<?php
//Database Config file
require_once("conn.php");

if(isset($_POST['Submit'])){

$Name=$_POST['name'];
$Password1=$_POST['password'];
$Username=$_POST['username'];
$Email=$_POST['email'];

//md5 password
$Password=md5($Password1);
$activation = md5(uniqid(rand(), true));//generate random numbers

//Insert values in table
$res="INSERT into register (Name,Email,Username,Password,activation)VALUES('$Name','$Email','$Username','$Password','$activation')";
$result=mysql_query($res);

if($res){

//Confirmation mail
    $to = $Email;
    $subject = 'Registration Confirmation'; //subject
    $message = " Hi,To activate your account, please click on this link:\n\n";
    $message .= 'http://www.yourwebsite.com/activation.php?email=' . urlencode($Email) . "&key=$activation" ;

$headers = "From:info@yourwebsite.com\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
    $headers .= "X-Priority: 1\n";
    $headers .= "X-MSmail-Priority: High\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;

//Mail Script
$sentmail=mail($Email, $subject, $message,$headers);

echo "Thank you registering with us.!";
}
else
{
echo "Error.Please try again";
}
}

?>
<form action="#" id="register" method="post">
  <table border="0">
    <tbody>
      <tr>
        <td><label for="name">Your Name: </label>
        </td>
        <td><input id="name" maxlength="45" name="name" type="text" />
        </td>
      </tr>
      <tr>
        <td><label for="email">Email:</label>
        </td>
        <td><input id="email" maxlength="45" name="email" type="text" /></td>
      </tr>
      <tr>
        <td><label for="username">User Name:</label>
        </td>
        <td><input id="username" maxlength="45" name="username" type="text" />
        </td>
      </tr>
      <tr>
        <td><label for="password">Password:</label></td>
        <td><input id="password" maxlength="45" name="password" type="password" /></td>
      </tr>
      <tr>
        <td align="right"><input name="Submit" type="submit" value="Submit" /></td>
      </tr>
    </tbody>
  </table>

</form>

activation.php

<?php
//Database Config file
require_once("conn.php");
//print_r($_GET);

if (isset($_GET['email']) && preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $_GET['email'])){
    $email = $_GET['email'];
}
if (isset($_GET['key']) && (strlen($_GET['key']) == 32)){ //The Activation key will always be 32 since it is MD5 Hash
    $key = $_GET['key'];
}

if (isset($email) && isset($key)) {
    // Update the database to set the "activation" field to null
    $result_activate_account = mysql_query("UPDATE register SET activation=NULL WHERE(Email ='$email' AND activation='$key')");
    // Print a customized message:
    if ($result_activate_account){ //if update query was successfull
   $sMsg = 'Your account is now active. You may now log in';
   echo $sMsg;
    } else {
        $sMsg = 'Oops !Your account could not be activated. Please recheck the link or contact the system administrator.';
        echo $sMsg;
}
}

?>


3 comments: