我在使用 check_login 函数时遇到问题。 当我输入 http://localhost/home/并输入管理员密码,它不会重定向到主页管理页面。但是当我尝试 http://localhost/home/control/lampu .它仍然重定向到登录页面。
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('loginModel');
$this->load->model('pengaturanModel');
$this->form_validation->set_error_delimiters("toastr.error('", "','');");
}
public function index()
{
if($this->session->userdata('id_pengaturan')){
redirect('control/lampu');
}
$data['pesan'] = '';
$this->form_validation->set_rules('kodeakses', 'Kode Akses Keamanan', 'trim|required|md5');
if ($this->form_validation->run() == FALSE)
{
}
else
{
$data['pesan'] = "toastr.error('Kode Akses Keamanan Tidak Boleh Kosong!','')";
$check = $this->loginModel->get_records($this->input->post('kodeakses'));
if($check){
foreach ($check as $row) {
$login = array(
'id_pengaturan' => $row->id,
);
$this->session->set_userdata($login);
redirect('control/lampu');
}
}else{
$data['pesan'] = "toastr.error('Kode Akses Keamanan Salah!','')";
}
}
$this->load->view('login', $data);
}
}
还有这段代码登录模型
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class LoginModel extends CI_Model {
function get_records($kodeakses){
$query = $this->db->get_where('pengaturan', array('kode_akses'=> $kodeakses), 1);
if($query->num_rows() == 1){
return $query->result();
}else{
return false;
}
}
function getKode($kodeakses){
$query = $this->db->get_where('pengaturan', array('kode_akses'=> $kodeakses), 1);
if($query->num_rows() == 1){
return true;
}else{
return false;
}
}
}
/* End of file login_m.php */
/* Location: ./application/models/login_m.php */
最佳答案
这是因为 num_rows()
不是查询构建器函数的一部分。您可以尝试使用它来计算您的结果:
$this->db->count_all_results()
引用 - https://www.codeigniter.com/user_guide/database/query_builder.html#limiting-or-counting-results
如果这解决了您的问题,请将其标记为答案。谢谢!
关于php - 检查 Codeigniter 中的登录和重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46078887/