0

I want to display the name of the registered user on other page. I can display the username, user_id, but I want to display the name of registered user. This is my controller:

code:

    public function login() {
        $data['title'] = 'Sign In';

        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header');
            $this->load->view('users/login', $data);
            $this->load->view('templates/footer');

        }
        else {
            //Get username
            $username = $this->input->post('username');
            
            //Get and encrypt password
            $password = md5($this->input->post('password'));
            
            //Login user
            $user_id = $this->user_model->login($username, $password);


            if($user_id) {

                //Create session
                $user_data = array(
                    'user_id' => $user_id,
                    'username' =>$username,
                    'logged_in' => true
                );

                $this->session->set_userdata($user_data);

                //Set message
                $this->session->set_flashdata('user_loggedin', 'You are now logged in');

                redirect('dashboard/index');
            }
            else {
            //Set message
            $this->session->set_flashdata('login_failed', 'Login is invalid');

            redirect('users/login');    
            }

        } 
    }

This is my model :

code

       public function login($username, $password) {
        //Validate
        $this->db->where('username', $username);
        $this->db->where('password', $password);

        $result = $this->db->get('users');
        if($result->num_rows() == 1){
            return $result->row(0)->id;
        }
        else {
            return false;
        }
    }

This is my view:

code

      <p>Welcome, <?php echo $this->session->username ?> </p>

I have tried the same for name, even adding in the user_data array above. But, somehow it only works for username, user_id and logged_in value.

  • *I want to display the name of the registered user*: have you written some code to do that? – Vickel Mar 31 '22 at 22:41
  • in addition to your question: using md5 for password hash is considered unsafe, check [Secure hash and salt for PHP passwords](https://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords). Use php built in [password_hash()](https://www.php.net/manual/en/function.password-hash.php) instead – Vickel Mar 31 '22 at 22:42
  • instead of re-inventing the wheel, consider to use an existing auth library like [ion-auth](https://github.com/benedmunds/CodeIgniter-Ion-Auth) or others – Vickel Mar 31 '22 at 22:44
  • what you means by `registered user`? , is `user` and `registered user` are same or different? – KUMAR Apr 01 '22 at 01:07

1 Answers1

1

I assume you have a column 'name' in your DB.

In your model

return $result->row();

instead of

$result->row(0)->id;

set session in controller to add name in session

$user_data = array(
'user_id' => $user_id->id,
'name' =>$user_id->name,
'username' =>$username,
'logged_in' => true
);

On your view

<p>Welcome, <?php echo $this->session->name ;?> </p>
CaPs LoCk
  • 150
  • 4