douzhi2012 2018-10-11 07:17
浏览 24

我的codeigniter登录代码工作了一段时间并停止

My codeigniter login code works for a time and then stops. And I noticed also that anytime I clear my browser cache and other parameters, it also stops working until I change the username and password again.

This is my controller code

// Log in user
    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('admins/index', $data);
            $this->load->view('templates/footer');
        } else {

            // Get username
            $username = $this->input->post('username');
            // Get and encrypt the password
            $password = md5($this->input->post('password'));

            // Login user
            $user_id = $this->admin_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
                //set message
                $this->session->set_flashdata('admin_loggedin', 'Welcome, You are now logged in');

                redirect('admins/view');
            } else {
            // Set message
                $this->session->set_flashdata('login_failed', 'Oops, Your username or password is not correct!');

                redirect('admins');
            }       
        }

And this is my model code

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

        $result = $this->db->get('admins');

        if($result->num_rows() == 1){
            return $result->row(0)->id;
        } else {
            return false;
        }
    }

This is my .htaccess code

 RewriteEngine On
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^system.*
  • 写回答

1条回答 默认 最新

  • duanjian7343 2018-10-11 07:26
    关注

    If think that, by default, CodeIgniter uses sessions to store logged in user. So, this might be your scenario - by clearing the browser cache, you clear the cookie that stores the session ID and the session gets invalidated, hence causing you to be logged out.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用