0

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

提前致谢。

4

1 回答 1

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();
        }
    }
于 2012-07-11T00:01:29.443 回答