1

我已经这样做了一段时间无济于事。我在使用 codeigniter 引导登录时遇到问题。所以基本上这就是我所拥有的:

login.php - 控制器

 <?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');?>

找不到对象。我在这里缺少什么?任何帮助深表感谢。

4

3 回答 3

1
 class Login extends Main_Controller {

应该是

 class Login extends CI_Controller {

除非你已经有 Main_controller 扩展 CI

于 2013-07-18T17:07:36.120 回答
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 中管理会话长度和其他属性。

让我知道它是否有帮助。

于 2013-07-18T17:43:44.537 回答
0

如果您不使用 .htaccess 来删除此处显示的 index.php http://ellislab.com/codeigniter/user-guide/general/urls.html然后试试这个

<?php echo base_url('index.php/login/process');?>
于 2013-07-18T17:24:13.810 回答