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条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?