duanmu5641 2013-07-18 16:55
浏览 49
已采纳

codeigniter bootstrap登录表单问题

I've been doing this for a while to no avail. I'm having an issue with codeigniter bootstrap login. So basically this is what I have:

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 - view

<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 - model

  <?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;
 }
 }
?>

I get an error in

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

Object is not found. What am I missing in here? Any help is much appreciated.

  • 写回答

3条回答 默认 最新

  • dongtan8979 2013-07-18 17:43
    关注

    Try replacing

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

    with just

    '/login/process'
    

    Don't forget the leading slash. This is just a guess, because we don't have enough informations provided about your settings.

    EDIT::

    NOTE: Read my comment first.

    First in your autoload.php load session library. Then modify your process() function in controller like this:

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

    Then in your home.php controller:

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

    You need to set the session so the member area knows that user is logged in. You can manage session length and other properties in config.php.

    Let me know if it helped.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效