我正在尝试学习 MVC 和 codeigniter,并试图找出我做错了什么。我正在尝试使用包含电子邮件和密码的非常简单的表单登录。在控制器中,我首先加载包含表单的视图。填写完所有内容并单击“loginSubmit”按钮后,它应该转到我的控制器中的“login()”。
在我的方法“login()”中,我加载了模型,在这里我调用了“validate()”方法。当它返回某些内容时,我向控制器返回 true 以启动会话并将我重定向到配置文件页面。
现在的问题是,无论我要登录什么,它总是用登录方法刷新登录控制器
所以我浏览到
http://localhost/project/index.php/login/
当按下提交时让我
http://localhost/project/index.php/login/login/
为什么不做任何检查,发生了什么?
登录视图
<?php
$loginEmail = array('placeholder' => "Email", 'name' => "loginEmail");
$loginPassword = array('placeholder' => "Wachtwoord", 'name' => "loginPassword");
$loginSubmit = array('name' => "loginSubmit", 'class' => "btn", 'value' => "Inloggen");
$loginForgot = array('name' => "loginForgot", 'class' => "link", 'value' => "Wachtwoord vergeten?");
echo form_open('login/login', array('class' => 'grid-100 formc'));
echo form_input($loginEmail);
echo form_password($loginPassword);
echo form_submit($loginSubmit);
echo form_submit($loginForgot);
?>
登录控制器
<?php
Class Login extends CI_Controller{
public function __construct() {
parent::__construct();
}
function index(){
$data['content'] = 'login_view';
$this->load->view('templates/template', $data);
}
function login(){
$this->load->model('login_model');
$query = $this->login_model->validate();
if($query){
$data = array(
'username' => $this->input->post('loginEmail'),
'loggedin' => true
);
$this->session->set_userdata($data);
redirect('profile/myprofile');
}
else{
echo "not logged in";
}
}
}
?>
登录模型
<?php
Class Login_model extends CI_Model{
function __construct(){
parent::__construct();
}
function validate(){
$this->db->where('email', $this->input->post('loginEmail'));
$this->db->where('password', md5($this->input->post('loginPassword')));
// I also tried with get_where, but same effect. BTW. what is the difference between where() and get_where() and what is better?
//$query = $this->db->get_where('tbl_users', array(('email', $this->input->post('loginEmail'), ('password', $this->input->post('loginPassword')));
$query = $this->db->get('tbl_users');
if($query->num_rows == 1){
return true;
}
}
}
?>