簡體   English   中英

無法使用數據庫中的電子郵件和用戶名登錄(Codeigniter php)

[英]Cant login with email and username that is on database (Codeigniter php)

我已經有了包含用戶名、電子郵件(主鍵)和密碼表的數據庫,並且已經填寫了它們。 但是當我嘗試將這些電子郵件和密碼放在我的網頁上時,它被認為是錯誤的。

這是我的登錄控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_controller extends CI_Controller {
    public function __construct() {
        parent::__construct();

        //load the required libraries and helpers for login
        $this->load->helper('url');
        $this->load->library(['form_validation','session']);
        $this->load->database();

        //load the Login Model
        $this->load->model('LoginModel', 'Login_controller');
    }
    public function index() {
        //check if the user is already logged in 
        $logged_in = $this->session->userdata('logged_in');
        if($logged_in){
            //if yes redirect to welcome page
            redirect(base_url().'HomeLoggedIn_controller/index/');
        }
        //if not load the login page
        $this->load->view('login/v_login');
    }
    public function doLogin() {
        //get the input fields from login form
        $email = $this->input->post('email');
        $password = sha1($this->input->post('password'));

        //send the email pass to query if the user is present or not
        $check_login = $this->Login_controller->checkLogin($email, $password);
        //if the result is query result is 1 then valid user
        if ($check_login) {
            //if yes then set the session 'loggin_in' as true
            $this->session->set_userdata('logged_in', true);
            redirect(base_url().'HomeLoggedIn_controller/index/');
        } else {
            //if no then set the session 'logged_in' as false
            $this->session->set_userdata('logged_in', false);

            //and redirect to login page with flashdata invalid msg
            //$this->session->set_flashdata('msg', 'Username / Password Invalid');
            redirect(base_url().'Login_controller/index/');            
        }
    }
    public function logout() {
        //unset the logged_in session and redirect to login page
        $this->session->unset_userdata('logged_in');
        redirect(base_url().'Login_controller/index/');
    }
}

登錄模式:

<?php
class LoginModel extends CI_Model
{
    public function checkLogin($email, $password) {
        //query the table 'users' and get the result count
        $this->db->where('email', $email);
        $this->db->where('password', $password);
        $query = $this->db->get('tb_user');
        return $query->num_rows();

    }
}

我嘗試使用網站上的控制器和模型,並且我已經更改了視圖頁面上的某些內容以將其與控制器同步。

您好,如果我可以為您提供正確的信息,您想驗證模型中的登錄詳細信息。 請在您的控制器中使用它。 我希望這會幫助你解決你的問題。


defined('BASEPATH') OR exit('No direct script access allowed');
class Login_controller extends CI_Controller {
    public function __construct() {
        parent::__construct();

        //load the required libraries and helpers for login
        $this->load->helper('url');
        $this->load->library(['form_validation','session']);
        $this->load->database();

        //load the Login Model
        $this->load->model('LoginModel', 'Login_controller');
    }
    public function index() {
        //check if the user is already logged in 
        $logged_in = $this->session->userdata('logged_in');
        if($logged_in){
            //if yes redirect to welcome page
            redirect(base_url().'HomeLoggedIn_controller/index/');
        }
        //if not load the login page
        $this->load->view('login/v_login');
    }
    public function doLogin() {
        //get the input fields from login form
        $email = $this->input->post('email');
        $password = sha1($this->input->post('password'));

        //send the email pass to query if the user is present or not
        $check_login = $this->LoginModel->checkLogin($email, $password);
        //if the result is query result is 1 then valid user
        if ($check_login) {
            //if yes then set the session 'loggin_in' as true
            $this->session->set_userdata('logged_in', true);
            redirect(base_url().'HomeLoggedIn_controller/index/');
        } else {
            //if no then set the session 'logged_in' as false
            $this->session->set_userdata('logged_in', false);

            //and redirect to login page with flashdata invalid msg
            //$this->session->set_flashdata('msg', 'Username / Password Invalid');
            redirect(base_url().'Login_controller/index/');            
        }
    }
    public function logout() {
        //unset the logged_in session and redirect to login page
        $this->session->unset_userdata('logged_in');
        redirect(base_url().'Login_controller/index/');
    }
}



發現錯誤! 你沒有很好地加載模型方法 checklogin

您使用了$this->Login_controller->checkLogin($email, $password); 而不是$this->LoginModel->checkLogin($email, $password); . 這導致你的錯誤。 請用我的代碼替換你的代碼。

如果不引起我的注意,我希望這對您有所幫助,好吧

嘗試改變

$check_login = $this->Login_controller->checkLogin($email, $password);

到:

$check_login = $this->checkLogin($email, $password);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM