codeigniter bootstrap登录表单问题

时间:2013-07-18 16:55:20

标签: php codeigniter twitter-bootstrap

我已经做了一段时间但无济于事。我遇到了codeigniter bootstrap登录问题。基本上这就是我所拥有的:

login.php - controller

 <?php if (!defined('BASEPATH')) die();
 class Login extends Main_Controller {

 function __construct(){
    parent::__construct();
 }

 public function index()
 { 
  $this->load->view('include/header');
  $this->load->view('login_view');
  $this->load->view('include/footer');
 }

 public function process(){
    // Load the model
    $this->load->model('login_model');
    // Validate the user can login
    $result = $this->login_model->validate();
    // Now we verify the result
    if(! $result){
        // If user did not validate, then show them login page again
        $this->index();
    }else{
        // If user did validate, 
        // Send them to members area
        redirect('home');
    }        
}

}

login_view.php - 查看

<div class="container">
  <form class="form-signin" action="<?php echo base_url('login/process');?>" method="post" id="loginForm">
    <h2 class="form-signin-heading"><img src="<?php echo base_url('assets/img/hms_icon.png'); ?>" />User Login</h2>
    <input type="text" class="input-block-level" placeholder="Username" name="username" id="username">
    <input type="password" class="input-block-level" placeholder="Password" name="password" id="password">
    <button class="btn btn-large btn-primary" type="submit" name="login" id="login">Sign in</button> 

    <div id="report"></div>
  </form>
 </div> <!-- /container -->

login_model.php - 模型

  <?php
 Class Login_model extends CI_Model
{

function __construct(){
    parent::__construct();
}

public function validate(){
    // grab user input
    $username = $this->security->xss_clean($this->input->post('username'));
    $password = $this->security->xss_clean($this->input->post('password'));

    // Prep the query
    $this->db->where('username', $username);
    $this->db->where('password', $password);

    // Run the query
    $query = $this->db->get('users');
    // Let's check if there are any results
    if($query->num_rows == 1)
    {
        // If there is a user, then create session data
        $row = $query->row();
        $data = array(
                '_id' => $row->userid,
                'username' => $row->username,
                'validated' => true
                );
        $this->session->set_userdata($data);
        return true;
    }
    // If the previous process did not validate
    // then return false.
    return false;
 }
 }
?>

我在

中收到错误
 <?php echo base_url('login/process');?>

找不到对象。我在这里想念的是什么?非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

 class Login extends Main_Controller {

假设是

 class Login extends CI_Controller {

除非您已经将Main_controller扩展为CI

答案 1 :(得分:0)

如果您不使用.htaccess删除此处显示的index.php http://ellislab.com/codeigniter/user-guide/general/urls.html,请尝试此操作

<?php echo base_url('index.php/login/process');?>

答案 2 :(得分:0)

尝试替换

<?php echo base_url('login/process');?>

只是

'/login/process'

不要忘记领先的斜线。这只是猜测,因为我们没有提供有关您的设置的足够信息。

修改::

注意:请先阅读我的评论。

首先在autoload.php加载会话库中。然后在控制器中修改process()函数,如下所示:

public function process(){
   $this->load->model('login_model');
   $result = $this->login_model->validate();
   if(! $result){
       $this->index();
   }else{
      $data = array(
          'username'    => $this->input->post('username'),
          'is_logged_in'    => true
   );

   $this->session->set_userdata($data);
   redirect('home');
}

然后在你的home.php控制器中:

function __construct()
    {
        parent::Controller();
        $this->is_logged_in();
        }

function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');

        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            redirect('login');
        }
    }

您需要设置会话,以便成员区域知道用户已登录。您可以在config.php中管理会话长度和其他属性。

如果有帮助,请告诉我。