[英]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.