Thursday, July 18, 2013

Developing an eMail validation system using PHP and MySQL

Well, its great that we do create websites that require sign up from users. To prevent random sign ups and span, developers have developed an email verification system. This system can be seen in almost every website throughout the WWW. In this article, I am going to show you how you can develop a simple script that validates email address.
Firstly, let me explain the database structure:
The name of the database is test, and the name of the table is email. The field email is considered unique. The activatecode contains the corresponding activation code for email generated while the user was signing up. The field activatestatus is self explanatory. It contains one character either Y or N to denote the activation status.

Now, recipe that we are going to use to cook this script is the HTTP GET. PHP can read the GET instruction from URL and convert it into an associative array. The same thing we are going to use in this tutorial.
Let's Code!
//Initializing database connection starts
$host = 'localhost';
$user = 'test';
$password = 'test';
$database = 'test';
$con = mysqli_connect($host, $user, $password, $database);
    echo mysqli_error ($con);
//connected to the database
//you can check if URL is in the correct format or not before whatever we are going to do here
$key = $_GET['key']; //key will be the URL parameter
$email = $_GET['email'];//for security, we will also consider email as the paramaeter
$query = "Select * from email where email='$email'";
$result = mysqli_query($con, $query);
$assoc = mysqli_fetch_assoc($result);//an associative array is fetched for better understanding
//as email is a unique field it will only fetch one 1D array
if($key = $assoc['activatecode']){
    $query = "update email set activatestatus='Y'";
    $update = mysqli_query($con, $query);
    if(!$update) echo mysqli_error ($con);
    else echo 'activation successful';
else echo 'Either the Email is wrong or the associated key is not with the email';

Now that we have coded, here's how to change the activatestatus of a given email. Just point your URL to

You email will be activated.
Download this script!
More coding at Let's Code
Warning: This is for testing purposes only. I am giving you this tutorial as an advanced example on how to use several functions in PHP and running appropriate querys in MySQL using PHP. Do not publish!