Skip to content
Advertisement

Codeigniter 3 application bug: logged in user’s avatar does not update in real time

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):

JavaScript

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:

JavaScript

In the Login controller I have:

JavaScript

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:

  1. In login controller you are setting up in array the session.
  2. with update_user in model you are updating your avatar.
  3. 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).

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement