Email Verification in PHP: Part 1


This video to illustrates, verification of user email ID by sending a unique URL to be clicked by the user inorder to active his account, to further use the application.

Use
We can restrict certain features of our application for the accounts which has not been verified.
By this we can know if the email ID is valid and authorized user is using it and no one else.

Database

1
2
3
4
5
6
7
8
9
mysql> create database test;
mysql> use test;
mysql> create table login(
   name varchar(25) not null, 
   pass varchar(60) not null, 
   email varchar(30) primary key,
   phn varchar(15), 
   rndm varchar(20) not null, 
   actv int not null);

Note: Do not make phno as integer, as it can’t handle huge numbers like 9844552841 (if its a phone number!)
email is concede red as Primary Key, as we can identify each user uniquely via this attribute. No two user can have the same email id.

Database connection script
db.php

1
2
3
4
< ?php
 mysql_connect('localhost','DatabaseUsername','DatabasePassword');
 mysql_select_db('DatabaseName');
?>

Registration Code:
register.php

1
2
3
4
5
6
7
<form action="save.php" method="post">
Name: <input type="text" name="n"/>
eMail ID: <input type="text" name="email"/>
password: <input type="password" name="password"/>
Phn number: <input type="text" name="phn"/>
<input type="SUBMIT"/>
</form>

The user input will be passed to save.php via post method.

save.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
< ?php include_once('db.php'); ?>
< ?php
 
$n = $_POST['n'];
$p = md5($_POST['password']);
$email = $_POST['email'];
$phn = $_POST['phn'];
$rndm = rand(34565, 993240);
 
$sql ="INSERT into login values('$n','$p','$email','$phn', '$rndm', '0')";
 
if(!mysql_query($sql))
 echo "Not updated..".mysql_error();
else
{
 
  $url = "http://yourdomainname.com/reg/activate.php?act=$rndm&email=$email";
  $cont = "click this link $url To activate your account";
 
  if( mail($email, "Activate Account!", $cont, "From: [email protected]") )
   echo '<b>Please visit your email to activate your account
         <br />After Activation, <a href="login.php">log into your account</a>';
  else
   echo '<b>Registration failed..<a href="register.php">please try again</a></b>';
 
}
?>

change yourdomainname.com to the actual domain name the application is hosted on.

md5() is a built-in function. It is a encryption technique for securing the user passwords from intruders.
rand( initialValue, finalValue) is a built-in PHP function which takes range as its parameter and generates a random number between them.

mail() is a built-in PHP function for sending emails.

General Syntax:
mail(To, Subject, Content, From)

Video Tutorial: Part 1


[youtube https://www.youtube.com/watch?v=BwgOn5sbK4c]

YouTube Link: https://www.youtube.com/watch?v=BwgOn5sbK4c [Watch the Video In Full Screen.]



Also look at: Email Verification in PHP: Part 2

You can purchase this simple script directly for as less as $1.95 get here.

6 thoughts on “Email Verification in PHP: Part 1”

  1. Hi satish, I paid 1.95 USD for script. But it did not come. Could you please send it. or How do I get it?

    1. @hamdi, Will mail you the files. It’s actually automated, not sure why you din’t get the mail. Will check and fix it too.
      Update: Files sent to your email.

Leave a Reply

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