Welcome Guest, Not a member yet? Register   Sign In
Blogging application bug: logged in user's avatar does not update in real time
#1

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



Code:
<?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. Sad
In the model I have:

Code:
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 Lofin 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?
Reply
#2

@Ajax30,

When do you activate session variables? When do you turn off the session variables?
Reply
#3

If you store it that way, no other user can see your avatar. You need to always look for it (and cache the page of course!).
Reply




Theme © iAndrew 2016 - Forum software by © MyBB