Email verification php source code

Email verification is a simple process there is two way to verify email either by sending code to user email address or by sending link both works same here is a sample code from a tutorial //talkerscode.com/webtricks/account-verification-system-through-email-using-php.php on TalkersCode

// Table Scheme for Verify Table
CREATE TABLE `verify` [
`id` int[11] NOT NULL AUTO_INCREMENT,
`email` text NOT NULL,
`password` text NOT NULL,
`code` text NOT NULL,
PRIMARY KEY [`id`]
] ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1

// Table Scheme for verified_user table
CREATE TABLE `verified_user` [
`id` int[11] NOT NULL AUTO_INCREMENT,
`email` text NOT NULL,
`password` text NOT NULL,
PRIMARY KEY [`id`]
] ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1


if[isset[$_POST['register']]]
{
$email_id=$_POST['email'];
$pass=$_POST['password'];
$code=substr[md5[mt_rand[]],0,15];
mysql_connect['localhost','root',''];
mysql_select_db['sample'];

$insert=mysql_query["insert into verify values['','$email','$pass','$code']"];
$db_id=mysql_insert_id[];

$message = "Your Activation Code is ".$code."";
$to=$email;
$subject="Activation Code For Talkerscode.com";
$from = 'your email';
$body='Your Activation Code is '.$code.' Please Click On This link Verify.php?id='.$db_id.'&code='.$code.'to activate your account.';
$headers = "From:".$from;
mail[$to,$subject,$body,$headers];

echo "An Activation Code Is Sent To You Check You Emails";
}

if[isset[$_GET['id']] && isset[$_GET['code']]]
{
$id=$_GET['id'];
$code=$_GET['id'];
mysql_connect['localhost','root',''];
mysql_select_db['sample'];
$select=mysql_query["select email,password from verify where id='$id' and code='$code'"];
if[mysql_num_rows[$select]==1]
{
    while[$row=mysql_fetch_array[$select]]
    {
        $email=$row['email'];
        $password=$row['password'];
    }
    $insert_user=mysql_query["insert into verified_user values['','$email','$password']"];
    $delete=mysql_query["delete from verify where id='$id' and code='$code'"];
}
}

In this article, I will discuss how to send an email from localhost and create a registration with email verification using php. 

I will do it in row php, and not in oop. Also, I will use bootstrap code to make it faster. 

Additional things we will need. 

  1. phpMailer library
  2. mailtrap.io account credential for sending emails

First, let’s create a database and a table. The database name is `verification`  and table name is `users`.

The columns which I will create to validate is, 

Id, email, password,code, is_verified.

This is the code to create the table.

CREATE TABLE `verification`.`verification` [ `id` INT[11NOT NULL AUTO_INCREMENT , `name` VARCHAR[50NOT NULL , `email` VARCHAR[60NOT NULL , `code` VARCHAR[10NOT NULL , `is_verified` TINYINT[3NOT NULL DEFAULT '0' , PRIMARY KEY [`id`]] ENGINE = InnoDB; 

Now let's move to the coding part. 

First I am creating a folder named as verification inside my local server folder. It might be www/htdocs based on your server app[wamp/xampp/ampps etc ]

Now inside this `verification ` folder I am creating those files. 

  1. Registration.php
  2. Verify.php
  3. Db.php
  4. sendEmail.php

Lets start with the db.php. In this file we will connect to the database. 

Here is the code for db.php

So our database is connected now.

Lets download the PHPmailer and configure it for sending email. To download it go to this link: PHPMailer/PHPMailer: The classic email sending library for PHP [github.com]

Once downloaded, rename it to PHPMailer and move this folder inside our project folder. Our project folder name was ` verification `. So put this phpMailer inside this folder.

So our project folder looks like this.

Now our mailer library is also integrated. Let’s configure the mailer code.

Before configuring, let's get the mailtrap.io credential. To get this, login to your mailtrap.io account and get the credential as mentioned in the screenshot below. 

Now open sendEmail.php and paste the below code. 

Bài mới nhất

Chủ Đề