由于我是代码点火器和MVC的新手,我很慢但肯定会通过我存储在数据库中的数据来记录用户的登录表单。
我已经从论坛的文章和建议拼凑了我当前的版本,但是我在这一点上有点困惑,为什么数据似乎没有被正确存储和发送。
如果密码不正确,我的登录表单设置为重新加载表单(最终会添加消息)
话虽如此,当我输入正确的登录信息时,它会重新加载页面。这只能告诉我,我没有正确地与数据库通信,或者没有正确地发送和/或存储我的$data
变量信息。
这是我的代码明智:
型号:
<?php
class User_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function login($data = array())
{
// validate data
if( !empty($data) ) return FALSE;
// retrieve query
$query = $this->db
->from('users')
->where($data)
->get();
// Check if query row exists
if($query->row())
{
// Query row exists, return query row
return $query->row();
}
else
{
// Query row doesn't exist, return FALSE
return FALSE;
}
}
}
查看:
title>Login</title>
<!--MAKE SURE SIGNED OUT HEADER IS IMPLEMENTED FOR ALL SIGNED OUT PAGES INCLUDING THIS ONE-->
<div class="structure clearfix">
<h1 class="title_header">
Sign In
</h1>
<div id="signin_form">
<?php
echo validation_errors();
echo form_open('auth/validate_credentials');
echo "<div class='form_text_signin'>";
echo "Email";
echo "</div>";
echo form_input('email');
echo "<div class='form_text_signin'>";
echo "Password";
echo "</div>";
echo form_input('password');
echo form_submit('submit', 'Submit');
echo form_close();
?>
</div>
</div>
控制器:
<?php
class Auth extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
// this is automatically called if no other function is called
// it simply turns around and calls the login() function to show the login page
public function index() {
$this->login();
}
public function login() {
$data['main_content'] = 'auth/login';
$this->load->view('includes/templates/main_page_template', $data);
}
function validate_credentials () {
$query = $this->load->model('user_model');
if($query)
{
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('account/dashboard');
}
else
{
$this->index();
}
}
}
提前感谢。
答案 0 :(得分:1)
看起来你在任何时候都没有从你的user_model类调用login方法 - 你只是在实例化类。你应该从你的控制器调用模型方法 - 我没有测试过这段代码,但希望它能让你走上正确的轨道:
function validate_credentials () {
$this->load->model('User_model');
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$query = $this->User_model->login($data);
if($query)
{
$data['is_logged_in'] = true;
$this->session->set_userdata($data);
redirect('account/dashboard');
}
else
{
$this->index();
}
}