Saturday, June 7, 2014

How to send activation link to email on user registration in PHP (Email verification)

During user registration in order to verify the email address of the user, a confirmation email is sent to the provided email address with activation link and when user clicks the link provided in the mail, his account gets activated. If you are looking for such PHP code. Then read this tutorial carefully.

This is a basic code to implement email verification using PHP and MySQL. In this code I have shown how to get a link to user email. The link actually contains an id which is stored in the database for each user. Here we use PHP mail() function to send an email to the user after registration.
You may also read:

PHP contact form using mail function using JQuery toggle. 

Beautiful contact form in PHP. 

Here is the MySQL query to create users table with five columns id, name, email, password and status. We are using the first column "id" in activation link and status contains whether the user has been activated or not.
Dump the below sql code into your MySql database.
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(70) NOT NULL,
  `status` int(11) NOT NULL DEFAULT '0',
   PRIMARY KEY (`id`)

Follow the below given steps.
Step 1. Look How to connect MySql database using PHP and copy the full PHP code from the block and save the file as db.php.

Step 2. Copy the below code and save it as register.php
<form action="" method="post" >
    <input type="text" name="name" >
    <input type="text" name="email">
    <input type="password" name="pwd" id="pwd">
    <input type="submit" name="submit" value="Register">
              $email = $_POST['remail'];
              $check = mysql_query("SELECT email FROM users where email='$email'");
              $num_rows = mysql_num_rows($check);
              if($num_rows > 0)
                  echo "<script>alert('Email Already in Use. Please Choose another Email')</script>";
                  $name = $_POST['name'];
                  $email = $_POST['email'];
                  $pwd = $_POST['pwd'];
                  $insert = mysql_query("INSERT INTO users(name,email,password) VALUES('$name','$email','$pwd')");
                $id = mysql_insert_id($conn);    //gets the last inserted id       
                $message = "<html>
                <p>Hello ".$name.",</p>
                <p>Your content here</p>
                <p><a href=\"http://DOMAIN-NAME/activate.php?actid=".$id."\">http://DOMAIN-NAME/activate.php?actid=".$id."</a></p>
                $from = "your-mail-id"; //Ex:
                $headers ='Reply-To: '. $from . "\r\n" ;
                $headers.='From: '.$from. "\r\n";
                $headers .='X-Mailer: PHP/' . phpversion();
                   $headers .= "MIME-Version: 1.0\r\n";
                $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
                  mail($email, "Activate your account", $message, $headers);

Step 3. Copy the below code and save it as activate.php
              $activationid = $_GET['actid'];
              $check = mysql_query("SELECT status FROM users where id='$actid'",$api->db);
              $num_rows = mysql_num_rows($check);
            if($num_rows > 0)
                $row = mysql_fetch_array($check);
                $status = $row['status'];
                $query = mysql_query("UPDATE users SET status = 1 where id = $actid",$api->db) or die(mysql_error());
            else { echo "<script>alert(Activation link is not valid.')</script>"; }
how to implement email verification system using PHP.
How to send activation link to email PHP
Email verification using PHP
Email verification script
User registration email check verify validate with PHP