Generate token key in the registration of CodeIgniter application

In this post I am going to explain generate token key in the registration of CodeIgniter application, Now in every application after registration into that application we will get confirmation mail with token key or confirmation key. In this post we are going to know about generation of key and confirmation in CodeIgniter application.

Generate token key in the registration of CodeIgniter application by Anil Kumar Panigrahi

Generate token key in the registration of CodeIgniter application by Anil Kumar Panigrahi

Users Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {
       public function __construct(){
           parent::__construct();
       }
       public function register(){
       }
       public function confirm(){
       }
}
?>
demo link for Generate token key in the registration of CodeIgniter application

In this controller, Register method:

public function register()
    {
        $this->load->view('templates/header');
        $this->load->view('users/register');
        $this->load->view('templates/footer');
       
        $this->load->library('form_validation');

       
        if(!empty($this->input->post('email'))){
                $this->load->model('User_model');  
                if($this->User_model->getUserEmail($this->input->post('email')))
                {  
                    $data = array(
                       'fname' => $this->input->post('fname'),
                       'lname' => $this->input->post('lname'),
                       'email' => $this->input->post('email')
                    );
                    $this->db->insert('users', $data);
                    $userId = $this->db->insert_id();
                   
                    $this->load->model('Ticket_model');
                    $tktToken = $this->Ticket_model->generateToken($userId);
               
                    $this->sendmail($userId,$this->input->post('email'),$this->input->post('name'),$tktToken,'register','Verify your email');
                    redirect('users/index', 'refresh');
                }else{
                    $this->session->set_flashdata('msg', 'This email is already used, please try again with different account ...');
                    redirect('users/register', 'refresh');
                }
           
        }
    }

You can use html templates to send mail, which already explained in previous mail i.e. send email using HTML templates in Codeigniter.

Token key Generation code:

In the below code we generate the token and inserting into Database table.

<?php
class Ticket_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
   
    public function generateToken($userId,$tktReason='register'){
        $static_str='AL';
        $currenttimeseconds = date("mdY_His");
        $token_id=$static_str.$userId.$currenttimeseconds;
        $data = array(
                 'tktToken' => md5($token_id),
                 'tktReason' => $tktReason,
                 'userId' => $userId,
                 );
     $this->db->insert('tickets', $data);
        return md5($token_id);
     }
}
?>

After token generation mail will be send to respective user’s email with token key link. We can confirm that token with below code

Token key confirmation code

public function confirm(){
        $this->load->model('Ticket_model');
        $userId = $this->Ticket_model->checkToken($this->uri->segment(3),'register');
       
        if($userId !=''){
            $sql = "SELECT * FROM users WHERE userId = ?";
            $results = $this->db->query($sql, array($userId));
            $loginData = $results->row();
           
            $dataupdate = array(
                   'updated'=> gmdate("Y-m-d H:i:s"),
                   'status'=>1
                   );
            $this->db->where('userId', $userId);
            $this->db->update('users', $dataupdate);
               
            $this->db->where('tktToken', $this->uri->segment(3));
            $this->db->where('tktReason', "register");
            $this->db->delete('tickets');
            $sess_array = array(
                                'userId' => $loginData->userId,
                                'email' => $loginData->email
                                );
               $this->session->set_userdata('logged_in', $sess_array);
               $this->session->set_flashdata('msg', 'You has been confirmed as member');
               redirect('users/index', 'refresh'); 
        }
        else{
            $this->session->set_flashdata('msg', 'Your request process not success, You entered with an incorrect code');
            redirect('users/index', 'refresh');
            }
       
    }

Hope it will help in your CodeIgniter applications.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online

Contact : [email protected]

You may also like...

2 Responses

  1. priya says:

    i did’nt understand your code can you help me please. thanks

  2. Paul says:

    you forgot to include the checkToken function on your Ticket_model. kindly include it thanks

Leave a Reply

Your email address will not be published. Required fields are marked *