Saturday, June 7, 2014

Share It

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.
CREATE TABLE IF NOT EXISTS `users` (
  `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`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

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" >
    <label>Name</label>
    <input type="text" name="name" >
    <label>Email</label>
    <input type="text" name="email">
    <label>Password</label>
    <input type="password" name="pwd" id="pwd">
    <input type="submit" name="submit" value="Register">
</form>
<?php
        require_once("db.php");
          if(isset($_POST['submit']))
          {
              $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>";
              }
              else
              {
                  $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>
                <head></head>
                <body>
                <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>
                </body>
                </html>";
                $from = "your-mail-id"; //Ex: name@domain.com
                $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
  <?php
            require_once("db.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
Author: Bhanu Chander Bathini (CEO, Tricks Town.)
Hey friends, I am Bhanu Chander a Web Designer/Developer, Content writer, Freelance SEO analyst and a blogger. I am pursuing my M.Tech in Computer Science and Engg. You can contact me on bhanu4funn@gmail.com for web design solutions at low cost with effective SEO.

0 comments: