PHP Codeigniter url routing: token for registering process in the url
17:55 15 Jan 2017

Right after the registration process there is a link shown to set password: http://website.de/main/complete/token/MTkyYTBiY2EyNWE2ZDYwOTdhN2U4NjJkMjZmYzYyNTA

The problem is I can't display this page to complete the registration process.

Does anyone have a clue why? I'm already looking since 2 hours for it...

Controller function:

       public function complete()
        {                                   
        $token = base64_decode($this->uri->segment(4));       
        $cleanToken = $this->security->xss_clean($token);

        $user_info = $this->user_model->isTokenValid($cleanToken); //either false or array();           

        if(!$user_info){
            $this->session->set_flashdata('flash_message', 'Token is invalid or expired');
            redirect(base_url().'login');
        }            
        $data = array(
            'firstName'=> $user_info->first_name, 
            'email'=>$user_info->email, 
            'user_id'=>$user_info->id, 
            'token'=>$this->base64url_encode($token)
        );

        $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]');              

        if ($this->form_validation->run() == FALSE) {   
            $this->load->view('header');
            $this->load->view('complete', $data);
            $this->load->view('footer');
        }else{

            $this->load->library('password');                 
            $post = $this->input->post(NULL, TRUE);

            $cleanPost = $this->security->xss_clean($post);

            $hashed = $this->password->create_hash($cleanPost['password']);                
            $cleanPost['password'] = $hashed;
            unset($cleanPost['passconf']);
            $userInfo = $this->user_model->updateUserInfo($cleanPost);

            if(!$userInfo){
                $this->session->set_flashdata('flash_message', 'There was a problem updating your record');
                redirect(base_url().'login');
            }

            unset($userInfo->password);

            foreach($userInfo as $key=>$val){
                $this->session->set_userdata($key, $val);
            }
            redirect(base_url());

        }
    }


    public function register()
    {

        $this->form_validation->set_rules('firstname', 'First Name', 'required');
        $this->form_validation->set_rules('lastname', 'Last Name', 'required');    
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');    

        if ($this->form_validation->run() == FALSE) {   
            $this->load->view('header');
            $this->load->view('register');
            $this->load->view('footer');
        }else{                
            if($this->user_model->isDuplicate($this->input->post('email'))){
                $this->session->set_flashdata('flash_message', 'User email already exists');
                redirect(base_url().'login');
            }else{

                $clean = $this->security->xss_clean($this->input->post(NULL, TRUE));
                $id = $this->user_model->insertUser($clean); 
                $token = $this->user_model->insertToken($id);                                        

                $qstring = $this->base64url_encode($token);                    
                $url = base_url() . 'complete/token/' . $qstring;
                $link = '' . $url . ''; 

                $message = '';                     
                $message .= 'You have signed up with our website
'; $message .= 'Please click: ' . $link; echo $message; //send this in email exit; }; } }

Model functions:

    public function insertToken($user_id)
    {   
    $token = substr(sha1(rand()), 0, 30); 
    $date = date('Y-m-d');

    $string = array(
            'token'=> $token,
            'user_id'=>$user_id,
            'created'=>$date
        );
    $query = $this->db->insert_string('tokens',$string);
    $this->db->query($query);
    return $token . $user_id;

}

public function isTokenValid($token)
{
   $tkn = substr($token,0,30);
   $uid = substr($token,30);      

    $q = $this->db->get_where('tokens', array(
        'tokens.token' => $tkn, 
        'tokens.user_id' => $uid), 1);                         

    if($this->db->affected_rows() > 0){
        $row = $q->row();             

        $created = $row->created;
        $createdTS = strtotime($created);
        $today = date('Y-m-d'); 
        $todayTS = strtotime($today);

        if($createdTS != $todayTS){
            return false;
        }

        $user_info = $this->getUserInfo($row->user_id);
        return $user_info;

    }else{
        return false;
    }

}    
php codeigniter url routes codeigniter-3