【问题标题】:Online user status on CodeigniterCodeigniter 上的在线用户状态
【发布时间】:2017-12-31 05:37:41
【问题描述】:

我想知道如何在用户登录时更改在线用户状态。我一直在寻找这个,但我的问题没有答案。我是学习这个codeigniter的新手,所以希望你们能帮我解决我的问题,拜托。

控制器:

public function signdevice() {
    $device = $this->input->post('device');
    $pass = $this->input->post('password');
    $data = array(
        'device'   => $device,
        'password' => $pass,
        'status'   => $status
    );
    $status = 1;
    $res = $this->db->get_where('devices', $data);
    if ($res->num_rows() > 0) {
        $this->session->set_userdata(
            array(
                'signin' => TRUE,
                'device' => $device,
                'status' => $status
            )
        );
        $data = array('device' => $device, 'password' => $pass, 'status' => 1);
        $this->db->where('device', '$device');
        $this->db->update('devices', $data);
        redirect(base_url('pelanggan'));
    } else {
        $this->session->set_flashdata('result_login', 'Wrong Username or Password.');
        redirect(base_url());
    }
}

型号:

function login($user,$pass)
{
    $this->db->where('device', $user);
    $this->db->where('password', $pass);
    $query = $this->db->get('devices');
    if($query->num_rows()==1) {
        return $query->row_array();
    } else {
        $this->session->set_flashdata('error', 'Username atau password salah!');
        redirect('login');
    }
}

function update($id,$stat)
{
    $data['status'] = $stat;
    $stat = 1;
    $this->db->where('kd_device', $id);
    $this->db->update('devices', $stat);
    return TRUE;
}

【问题讨论】:

  • 除了没有专门初始化 $status = 0,你应该有什么工作?那么您的代码的实际问题是什么?是什么让您认为它不起作用。

标签: php codeigniter


【解决方案1】:

您有几行丢失和放错位置的代码。

控制器:

public function signdevice() {
    $device = $this->input->post('device');
    $pass = $this->input->post('password');
    $data = array(
        'device'   => $device,
        'password' => $pass,
        'status'   => 0 // changed value to 0
    );
    $status = 1;
    $res = $this->db->get_where('devices', $data);
    if ($res->num_rows() > 0) {
        $this->session->set_userdata(
            array(
                'signin' => TRUE,
                'device' => $device,
                'status' => $status
            )
        );
        $data = array('device' => $device, 'password' => $pass, 'status' => 1);
        $this->db->where('device', $device); // removed quotes
        $this->db->update('devices', $data);
        redirect(base_url('pelanggan'));
    } else {
        $this->session->set_flashdata('result_login', 'Wrong Username or Password.');
        redirect(base_url());
    }
}

型号:

...
function update($id,$stat)
{
    $stat = 1; // swapped this line with below
    $data['status'] = $stat; // swapped this line with above
    $this->db->where('kd_device', $id);
    $this->db->update('devices', $data); // maybe what you want to save is $data?
    return TRUE;
}

【讨论】:

  • 有趣的是,带有 update() 的模型从未使用过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多