I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
The application has user (author) accounts. There is a problem with displaying the photo (avatar) of the logged-in before the session is destroyed, due to the fact that I display the avatar from the session (header.php view):
<?php if ($this->session->userdata('user_avatar')): ?> <img src="<?php echo base_url('assets/img/authors/') . $this->session->userdata('user_avatar'); ?>" class="avatar" /> <?php else: ?> <img src="<?php echo base_url('assets/img/authors/') . 'default-avatar.png' ?>" class="avatar" /> <?php endif ?>
That seemed like a good idea at the time, an easy and logical implementation of the avatar display, until the update problem revealed. Of course, I have to logout and login again to see my avatar in the website’s header, as I had to admit. 🙁
In the model I have:
public function update_user($avatar, $id) { $data = [ 'first_name' => $this->input->post('first_name'), 'last_name' => $this->input->post('last_name'), 'email' => $this->input->post('email'), 'bio' => $this->input->post('bio'), 'avatar' => $avatar ]; $this->db->where('id', $id); return $this->db->update('authors', $data); }
In the Login controller I have:
public function login() { $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email'); $this->form_validation->set_rules('password', 'Password', 'required|trim'); $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>'); if ($this->form_validation->run()) { $email = $this->input->post('email'); $password = $this->input->post('password'); $this->load->model('Usermodel'); $current_user = $this->Usermodel->user_login($email, $password); // If we find a user if ($current_user) { // If the user found is active if ($current_user->active == 1) { $this->session->set_userdata( array( 'user_id' => $current_user->id, 'user_email' => $current_user->email, 'user_avatar' => $current_user->avatar, 'user_first_name' => $current_user->first_name, 'user_is_admin' => $current_user->is_admin, 'user_active' => $current_user->active, 'is_logged_in' => TRUE ) ); // After login, display flash message $this->session->set_flashdata('user_signin', 'You have signed in'); //and redirect to the posts page redirect('/'); } else { // If the user found is NOT active $this->session->set_flashdata("login_failure_activation", "Your account has not been activated yet."); redirect('login'); } } else { // If we do NOT find a user $this->session->set_flashdata("login_failure_incorrect", "Incorrect email or password."); redirect('login'); } } else { $this->index(); } }
What would be an easy to implement bugfix?
Advertisement
Answer
According to codeigniter session documentation, you can use something like this:
$this->session->set_userdata('user_avatar', $new_user_avatar);
Of course you need to add this in your code somewhere above the place you display the avatar.
For $new_user_avatar
you’ll even need to re-query the database after the avatar update, or you simple update the session after getting the form after you upload the file.
Acordingly to your code:
- In login controller you are setting up in array the session.
- with update_user in model you are updating your avatar.
- You are displaying in header view from session.
Your question is how to update the avatar. In the moment you use the update_user model, you have 2 options: 1. you also update the session with the specific avatar. 2. your run this function
$current_user = $this->Usermodel->user_login($email, $password);
array['user_avatar'] = $current_user->first_name
So final solution would be:
$this->session->set_userdata('user_avatar', $avatar);
should go after $avatar = $_FILES['userfile']['name'];
in the controller (line 91).