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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化