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

1
2
3
4
5
6
7
8
9
10
11
12
<?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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.


3 Comments

priya · December 30, 2016 at 11:44 am

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

Paul · March 30, 2017 at 7:49 am

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

Sachin · March 7, 2018 at 3:54 pm

How to get multiset results in codeigniter from store procedure

Leave a Reply

Avatar placeholder

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