【问题标题】:To check the account_status and log the account检查 account_status 并记录帐户
【发布时间】:2026-02-07 23:25:01
【问题描述】:

在下面的代码中,我在我的数据库中使用了 codeigniter,我有一个列名 account_status,默认情况下它将处于非活动状态。现在我想检查 account_status 如果它处于活动状态,它应该让我登录,否则不要。请帮助我做。

控制器:

function index()
    {
        $data['main_content'] = 'login_form';
        $this->load->view('includes/template', $data);      
    }

    function validate_credentials()
    {       
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();

        if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            redirect('site1/members_area');
        }
        else // incorrect username or password
        {
            $this->index();
        }
    }   

站点 1

function members_area()
    {
        $this->load->view('homepage_view');


    }

型号:

function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('membership');

        if($query->num_rows == 1)
        {
            return true;
        }

    }

【问题讨论】:

  • 你为什么不在模型中的 account_status 中使用另一个列
  • 我在 mysql 中手动创建了 account_status,所以当我创建一个帐户时它进入非活动状态

标签: php codeigniter login


【解决方案1】:

你有这些方法试试这个

只需在模型中使用 anther where 条件

function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $this->db->where('account_status','active');
        $query = $this->db->get('membership');

        if($query->num_rows == 1)
        {
            return true;
        }

}

或检查控制器中的状态

function validate_credentials()
    {       
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();

        if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            if($query->num_rows()>0)
             $status = $query->row()->account_status;
            else 
             $status = '';
            if($status == 'active')
            {
               $this->session->set_userdata($data);
               redirect('site1/members_area');
            }
            else //Account In active
            {  $this->index();  }
        }
        else // incorrect username or password
        {
            $this->index();
        }
    }   

【讨论】:

  • 感谢模型中的代码,它工作正常,但是当我在控制器中实现代码时,它显示错误:调用非对象上的成员函数 row()
  • 第 22 行显示相同的错误 Call to a member function num_rows() on a non-object in C:\wamp\www\seatingreport4\application\controllers\login.php
  • 哦,对不起,在您的模型中,您返回 $query 不正确,只需评论模型中的 if 部分。并返回 $query;