找不到404页面 - codeigniter登录

时间:2017-12-04 08:05:31

标签: php codeigniter login

我是codeigniter的初学者,我正在尝试通过引用一些在线vedios来做一个简单的登录表单。我做了一切。但当我尝试登录它显示我404页面未找到。请帮助我完成这个

autoload.php

$autoload['libraries'] = array('database','session');

routes.php文件

$route['default_controller'] = 'welcome';

控制器

的welcome.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {


public function __construct(){

    parent::__construct();
        $this->load->helper('url');
        $this->load->model('main_model');
        $this->load->library('session');
        $this->load->library('form_validation');}
public function index()
{
            $this->load->view('welcome_message');
}


function login_user(){
            $this->load->view('welcome_message');

}  function login_validation(){

            $this->load->library('form_validation');
            $this->form_validation->set_rules('username','Username','required');
            $this->form_validation->set_rules('password','Password','required');

            if($this->form_validation->run()){
                // true

                $username = $this->input->post('username');
                $password = $this->input->post('password');

                // model function
                $this->load->model('main_model');

                if($this->main_model->can_login($username, $password)){
                    $session_date = array(
                        'username'=> $username);

                    $this->session->set_userdata($session_date);

                    redirect(base_url() . 'main/enter');
                }else{
                    $this->session->set_flashdata('error','Invalid Username and Password');
                                 redirect(base_url() . 'main/login_user');

                }
            } else{
                //false

                $this->login_user();
            }}function enter(){
if($this->session->userdata('username') !=''){
echo '<h2> Welcome - '.$this->session->userdata('username').'/<h2>';

} else{redirect(base_url() . 'main/login_user');}}}

模型类

function can_login($username,$password){
$this->db->where('UserName',$username);
$this->db->where('Password',$password);
  $query=$this->db->get('User');


  if($query->num_rows() > 0){

    return true;

  } else{
   return false;
  }}

每当我尝试登录时,都会找不到404页面

的login.php

<form method="post" action="<?php echo base_url(); ?>main/login_validation">  
            <div class="form-group">  
                 <label>Enter Username</label>  
                 <input type="text" name="username" class="form-control" />  
                 <span class="text-danger"><?php echo form_error('username'); ?></span>                 
            </div>  
            <div class="form-group">  
                 <label>Enter Password</label>  
                 <input type="password" name="password" class="form-control" />  
                 <span class="text-danger"><?php echo form_error('password'); ?></span>  
            </div>  
            <div class="form-group">  
                 <input type="submit" name="insert" value="Login" class="btn btn-info" />  
                 <?php  
                      echo '<label class="text-danger">'.$this->session->flashdata("error").'</label>';  
                 ?>  
            </div>  
       </form>  

2 个答案:

答案 0 :(得分:0)

从未使用过这个框架,但对我来说似乎问题出在redirect或main / enter controller / action中。 要找出base_url()上的var_dump(),你可能会错误地调用它的类或静态方法。 在代码中的某处设置会话用户数据,而不是尝试进入main / enter。也许不存在?

编辑:似乎codeingniter重定向不需要base_ur只需使用

redirect('/main/enter');

答案 1 :(得分:0)

您的login_validation()方法位于Welcome控制器中。因此,form action welcome/login_validation应该是main/login_validation而不是private ArrayList<Customer> customers = new ArrayList<>(); public void addCustomer(String name){ Customer customer = new Customer(name); customers.add(customer); System.out.println("new customer " + customer.getName() + " added"); } public void deleteAccount(String name){ }

如果您想了解更多信息,可能需要阅读URLs in Codeigniter