php - 检查 Codeigniter 中的登录和重定向不起作用

标签 php codeigniter web

我在使用 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/

相关文章:

java - java android应用程序和PHP脚本之间交换数据最安全的方法?

php - Symfony 4 : use localizeddate

PHP session 不存储值

codeigniter - 基于一对多关系动态添加表单字段 - 有哪些框架替代方案?

java - 使用 Hibernate 与托管提供商创建 MySQL 数据库模式、优缺点、实践。

PHP 根据时间戳计算年龄和月份

mysql - 无法访问生产实例上的数据库

php - Laravel 框架对于中型项目是否足够稳定,还是我应该坚持使用更稳定的 Yii?

css - 使用 HTML 和 CSS 溢出到正文中?

JavaScript 多项选择问卷。如何更改代码以便可以勾选多个答案? (与第8个一起)