Codeigniter 3应用程序错误:登录的用户头像未实时更新

问题描述 投票:1回答:1

我正在使用Codeigniter 3.1.8Bootstrap 4开发基本的博客应用程序。

该应用程序具有用户(作者)帐户。由于我显示了会话中的头像(header.php视图),因此在会话被销毁之前显示登录照片(头像)存在问题。

<?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 ?>

在当时,这似乎是一个好主意,在显示更新问题之前,对虚拟形象显示进行简单而合理的实现。当然,必须承认,我必须注销并再次登录才能在网站标题中看到我的头像。 :(

在模型中,我有:

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);
    }

在登录控制器中,我有:

   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();
    }
}

什么是容易实现的错误修正?

我正在使用Codeigniter 3.1.8和Bootstrap 4开发基本的博客应用程序。该应用程序具有用户(作者)帐户。显示已登录的照片(头像)时出现问题...

php codeigniter codeigniter-3
1个回答
1
投票

根据codeigniter session文档,您可以使用类似以下的内容:

© www.soinside.com 2019 - 2024. All rights reserved.