dsndm82062 2016-12-15 11:07
浏览 76
已采纳

找不到对象! 在Codeigniter登录

I'm new to Codeigniter and I'm trying to build a login form by following some tutorials. However, when I try to submit the login form in order to validate the credentials that have been inputted by calling the validate_login function in the login controller , I get the "Object not Found" Error. Every time I click my submit button, my URL redirects to "http://localhost/pgevCI/login/validate_login", but it still gets the Object not Found error.

Controller: login.php

<?php

class Login extends CI_Controller {

    function index()
    {
        $data['main_content'] = 'view_login';
        $this->load->view('includes/login_template', $data);    
    }
function validate_login()
    {
        $this->load->model('model_accounts');
        $valid = $this->model_accounts->validate();

        if($valid)
        {
            $data = array(
                'userid' => $this->input->post('userid'),
                'is_logged_in' => true
            );

            $this->session->set_userdata($data); 
            redirect('site/admin');
        }

        else
        {
            $this->index();
        }       
      }
    }

View form: view_login.php

<form action="<?php echo base_url(); ?>login/validate_login" method="POST">
    <div class="row">
      <div class="col-lg-6">
        <div class="form-group">
          <input type="text" name="userid" class="form-control" id="user-name" aria-label="..." placeholder="Enter your username">
        </div><!-- /input-group -->
      </div><!-- /.col-lg-6 -->

      <div class="col-lg-6">
        <div class="form-group">
          <input type="password" name="password" class="form-control" id="user-password" aria-label="..." placeholder="Enter your password">
        </div><!-- /input-group -->
      </div><!-- /.col-lg-6 -->
    </div><!-- /.row -->

    </br>
    </br>
    <!-- Standard button -->
    <div class="action-buttons text-center">

      <button type="submit" class="btn btn-custom">Sign In</button>

      <button type="button" class="btn btn-custom">Forgot your password?</button>

    </div>
  </form>

Model: model_accounts.php

class Model_accounts extends CI_Model {

    function validate() 
    {

        $this->db->where('userid', $this->input->post('userid'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('users');

        if($query->num_rows == 1)
        {

            return true;    
        }
    }
}

Base URL & Index File :

$config['base_url'] = 'http://localhost/pgevCI/';
$config['index_page'] = 'index.php';

Error Page

Any help would really be appreciated! :)

  • 写回答

3条回答 默认 最新

  • dth981485742 2016-12-15 11:39
    关注

    You have to use

    <?php echo site_url('login/validate_login') ?>
    

    instead of

    <?php echo base_url(); ?>login/validate_login
    

    You haven't set CI to use nice URLs, so your real redirect url is

    http://localhost/pgevCI/index.php/login/validate_login
    

    You have to set

    $config['index_page'] = '';
    

    and use a htaccess if you want the urls to appear like

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!