由于我是代码点火器和 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();
}
}
}
提前致谢。