techfolks php tutorials logo
Codeigniter Email Verification Email Verification using Codeigniter Email Verification using PHP PHP Tutorials

Email Verification Confirmation Using PHP Codeigniter


Below is a simple program which shows email verification confirmation using a unique code, implemented in PHP,Mysql, Codeigniter.

PHP Codeigniter Email Verification

Step 1 :Create a table as shown below with email_verification_code as necessary column:

[mysql]
CREATE TABLE `users` (
`user_id` int(10) NOT NULL auto_increment,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`email_verification_code` varchar(45) NOT NULL,
`user_status` varchar(45) NOT NULL,
PRIMARY KEY (`user_id`)
);

[/mysql]

Step 2:Map your verify method of main controller using routing.

Add below below line code in routes.php file which is located at

“system/application/config/routes.php”$route[‘verify/(:any)’] = “/main/verify/$1”;

Step 3: Create controller class (main.php) which will send email to user for verification.

Add a user into users table, along with email_verification_code which has to be unique.

This unique email_verification_code value can be obtained from below code.

$verificationCode = random_string(‘alnum’, 20);

verify() is used to verify the email_verification_code which will be send confirmation code on clicking the link
Please click on below URL or paste into your browser to verify your Email Address.


"; $email_msg .= "http://yourdomain/user/verify/" . $verificationCode; 
$email_msg .= "Thanks, Support Team"; 

$subject = "Email Verification"; 
$this->load->library('email'); $config['charset'] = 'iso-8859-1'; 

$config['wordwrap'] = TRUE; $config['mailtype'] = 'html';

 $this->email->initialize($config); 

$this->email->from('admin@yourdomain.com', 'Support Team');

 $this->email->to($email); $this->email->subject($subject); 

$this->email->message($email_msg); $this->email->send(); // Insert user record 

} function verify($verificationText=NULL) { 

$this->load->model('user_model');

 $noOfRecords = $this->user_model->verifyEmailAddress($verificationText);

 if ($noOfRecords > 0) { 

$this->session->set_flashdata('success', "Email Verified Successfully! Please login below."); 

redirect('user/login'); 

} else { 

$this->session->set_flashdata('success', "Sorry! Unable to verify your email. Please try again."); 

redirect('user/login'); } } } ?> 

User will receive a confirmation email link, the user need to click on the confirmation link to complete the verification process.

Step 4: Verify the email_verification_code and update the user_status to 1 if the code is valid.

load->database();
   }
   public function verifyEmailAddress($verificationCode)
   {
    $this->db->set('user_status', 1)
     ->where('email_verification_code', $verificationCode)
     ->update('users');
    return $this->db->affected_rows();
   } 
  }

?>