Codeigniter 3.0.6 登录不工作

Codeigniter 3.0.6 Login not working

我是 CodeIgniter 的新手。我打算建立一个非常简单的 register/login 系统用于测试,但我从来没有通过注册的电子邮件和密码登录。

这是我的 Login_controller:

<?php

class Login_controller extends CI_Controller {

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

public function index($msg = NULL) {
    $data['msg'] = $msg;
    $this->load->view('login_view', $data);
}

public function process() {
    // Load the model
    $this->load->model('login_model');
    // Validate the user can login
    $result = $this->login_model->validate();
    // Now we verify the result
    if(! $result) {
        // If not valid user, then show them login page again
        $msg = 'Invalid email and/or password';
        $this->index($msg);
    }
    else {
        // If valid user, go to homepage
        redirect('home_view'); 
    }        
}
}

我的Login_model:

class Login_model extends CI_Model {
function __construct() {
    parent::__construct();
}

public function validate() {
    // Get user input
    $email = $this->input->post('email');
    $password = $this->input->post('password');

    // Prepare the query
    $this->db->select();
    $this->db->from('user');
    $this->db->where('email', $email);
    $this->db->where('password', $password);

    // Run the query
    $query = $this->db->get();

    // Check result
    if($query->num_rows > 0) {
        // If there is a user then create session data
        $row = $query->row();
        $data = array(
                'id' => $row->id,
                'username' => $row->username,
                'email' => $row->email,
                'validated' => true
                );
        $this->session->set_userdata($data);
        return true;
    }
    else {
    // If the previous process did not validate then return false
        return false;
    }
}
}

还有我的Login_view:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
    <h1> Login Zokzak </h1>

    <?php
        echo form_open('Login_controller/process');
    ?>

    Email: <input type="email" name="email"/> <br> <br>
    Password: <input type="password" name="password"/> <br>
    <input type="submit" value="Login"/>

    <?php
        if(!is_null($msg)) {
            echo $msg;
        }
    ?>
</body>
</html>

提前致谢!

首先,您必须区分加载视图和重定向页面。如果 home_viewAPPPATH.'views' 目录的文件 redirect('home_view'); 将不起作用。 Redirect function accepts REQUEST_URI string - in example redirect('controller_name/method_name/param1/param2'). View file is loaded by CI loader. For creating message you need to use flash session 将在下次请求时可用。

if(! $result) {
    // If not valid user, then show them login page again
    $this->session->set_flashdata('msg', 'Invalid email and/or password');
    redirect('login_controller');//index method is loaded by default
}
else
{
    $this->session->set_flashdata('msg', 'Successfully Logged In.');
    redirect('top_secret_controller/admin_dashboard_method');
}

Top_secret_controller.php中:

class Top_secret_controller extends CI_Controller
{
    private $logged_in = FALSE;

    public function __construct()
    {
        parent::__construct();
        $this->logged_in = ( isset($_SESSION['validated']) && $_SESSION['validated'] === 1 ) ? TRUE : $this->logged_in;//in your model use integer for validated value instead TRUE to [avoid confusing](https://dev.mysql.com/doc/refman/5.7/en/boolean-literals.html) with converting to integer since in DB it will be writen as 1

        $this->logged_in || redirected('login');//if not logged in redirect to login page
    }

    public admin_dashboard_method()
    {
        //code here
    }
}

快速 YT 搜索会给你一堆视频(比如 this one) about login system so you could compare good practices with your own. Personally, I am using Ion Auth system 不想发明轮子(除非我需要一个更圆的 :P )。