dongmi4035 2014-10-07 13:03
浏览 30

CodeIgniter登录表单不起作用

On my tvs system I try to make a login form. This form should result in a redirect to the admin panel, but something is missing and I could not figure it out. I have already put some information in the database "users" table. But any information from MySQL does not show even when I use var_dump();

My code in controller folder

class User extends Backend_Controller {

public function __construct() {
    parent::__construct();
}

public function login() {
    $this->load->helper('language');
    $this->lang->load('form_validation', 'lithuanian');
    $dashboard = 'admin/dashboard';
    $this->user_m->loggedin() == FALSE || redirect($dashboard);
    $rules = $this->user_m->rules;
    $this->form_validation->set_rules($rules);
    if ($this->form_validation->run() == TRUE) {
        if ($this->user_m->login() == TRUE) {
            redirect($dashboard);
        } else {
            $this->session->set_flashdata('error', 'Email/password are wrong');
            redirect('admin/user/login', 'refresh');
        }
    }
    $this->data['subview'] = 'admin/user/login';
    $this->load->view('admin/_layout_modal', $this->data);
}

public function logout() {
    $this->user_m->logout();
    redirect('admin/user/login');
}

}

In core folder MY_Model.php

class MY_Model extends CI_Model {

protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $rules = array();
protected $_timestamps = FALSE;

function __construct() {
    parent::__construct();
}

public function get($id = NULL, $single = FALSE) {
    if ($id != NULL) {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->where($this->_primary_key, $id);
        $method = 'row';
    } elseif ($single == TRUE) {
        $method = 'row';
    } else {
        $method = 'result';
    }

    if (!count($this->db->ar_orderby)) {
        $this->db->order_by($this->_order_by);
    }
    return $this->db->get($this->_table_name)->$method();
}

public function get_by($where, $single = FALSE) {
    $this->db->where($where);
    return $this->get(NULL, $single);
}

public function save($data, $id = NULL) {
    //timestamps
    if ($this->_timestamps == TRUE) {
        $now = date('Y-m-d H:i:s');
        $id || $data['created'] = $now;
        $data['modified'] = $now;
    }
    //insert
    if ($id === NULL) {
        !isset($data[$this->_primary_key]) || $data[$this->_primary_key] == NULL;
        $this->db->set($data);
        $this->db->insert($this->_table_name);
        $id = $this->db->insert_id();
    }
    //update
    else {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->set($data);
        $this->db->where($this->_primary_key, $id);
        $this->db->update($this->_table_name);
    }

    return $id;
}

public function delete($id) {
    $filter = $this->_primary_filter;
    $id = $filter($id);
    if (!$id) {
        return FALSE;
    }
    $this->db->where($this->_primary_key, $id);
    $this->db->limit(1);
    $this->db->delete($this->_table_name);
}

}

In model folder user_m.php

<?php

class User_M extends MY_Model {

protected $_table_name = 'users';
protected $_order_by = 'name';
public $rules = array(
    'email' => array(
        'field' => 'email',
        'label' => 'Email',
        'rules' => 'trim|required|valid_email|xss_clean'
    ),
    'password' => array(
        'field' => 'password',
        'label' => 'Password',
        'rules' => 'trim|required'
    )
);

function __construct() {
    parent::__construct();


  }

public function login() {

    $user = $this->get_by(array(
        'email' => $this->input->post('email'),
        'password' => $this->hash($this->input->post('password')),
            ), TRUE);
    if (count($user)) {
        //log in user
        $data = array(
            'íd' => $user->id,
            'email' => $user->email,
            'name' => $user->name,
            'loggedin' => TRUE,
        );
        $this->session->set_userdata($data);
    }
}

public function register() {

}

public function logout() {
    $this->session->sess_destroy();
}

public function loggedin() {
    return (bool) $this->session->userdata('loggedin');
}

public function hash($string) {
    return hash('sha512', $string . config_item('encryption_key'));
}

}

And in the view folder login.php

<?php
if (validation_errors() !== '') {
    echo "<div class='alert alert-warning alert-dismissible' role='alert'> <button type='button' class='close' data-dismiss='alert'><span aria-hidden='true'>&times;</span><span class='sr-only'>Close</span></button> <strong>Warning!</strong>";
    echo validation_errors();
    echo "</div>";
}
?>
<?php echo form_open(); ?>
<div class="form-signin">
    <?php
    $data = array(
        'name' => 'email',
        'class' => 'form-control',
        'placeholder' => 'Email'
    );
    echo form_input($data);
    $data = array(
        'name' => 'password',
        'class' => 'form-control',
        'placeholder' => 'Password'
    );
    echo form_password($data);
    echo form_submit('submit', $login, 'class="btn btn-lg btn-primary"');
    echo anchor('admin/user/auth/forgot_password', $forget_password, 'class="pull-right"');
    ?>
</div>
    <?php echo form_close(); ?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大