Search code examples
phpmysqlcodeignitercodeigniter-2codeigniter-3

Login codeigniter code not working


Please help. My codeigniter login using sessions is not logging nor able to logout.

When i click to login it gives me a 404 Page Not Found

The page you requested was not found.

My controller truckerlogin_ctrl

  <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class truckerlogin_ctrl extends CI_Controller {



 function __construct()
{
 parent::__construct();
}

 public function index()
{
$this->login_validation();
}

public function login_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('truckerid','truckerid','required');

if($this->form_validation->run()){
$data=array(
'truckerid'=>$this->input->post('truckerid'),
'is_logged_in'=>1
 );
 $this->session->set_userdata($data);
redirect('truckeraccount_view');
 }else{
 $this->load->view('truckerlogin_view');
 }
 }

  public function validate_credentials()
  {
  $this->load->model('Truckerlogin_model');
  if($this->Truckerlogin_model->can_log_in()){
   return true;
  }else{
   $this->form_validation->set_message('validate_credentials','Incorrect PIN');
    return false;
    }
    }

 public function logout()
    {
     $this->session->sess_destroy();
     redirect('truckerlogin_view');
    }


     }

     ?>

**My Model - truckerlogin_model.php **

   <?php

        class Truckerlogin_model extends CI_Model
      {

        public function can_log_in()
       {
         $this->db->where('truckerid',$this->input->post('truckerid'));
         $query=$this->db->get('trucker');
         if($query->num_rows()==1){
        return true;
       }else{
        return false;
       }
      }



     }
     ?>

My view truckerlogin_view.php

        <div class="login-box-body">
                 <?php if (isset($message)) { ?>

<h3 style="color:green;">Account created successfully</h3><br>
      <h3 style="color:red;">  <?php echo validation_errors(); ?> </h3>
      <div class="form-group has-feedback">
        <input type="text" class="form-control" name="truckerid" placeholder="Trucker PIN">
        <span class="fa fa-lock form-control-feedback"></span>
      </div>
      <div class="row">
        <div class="col-xs-4">
       <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
        </div><!-- /.col -->

                          <a href="<?php echo site_url('truckereginfo_ctrl/index/') ?>" class="text-center"> Register as a new member</a>                            




      </div>

Trucker Account truckeraccount_ctrl.php

  <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  session_start(); //we need to call PHP's session object to access it through   CI
      class truckeraccount_ctrl extends CI_Controller {

      public function __construct()
      {
       parent::__construct();
        $this->load->model('Truckeraccount_model');


        }

       public function index()
       {

        $this->$data['rows'] = $this->Truckeraccount_model->get_all();
        $this->load->view('truckeraccount_view', $this->$data);  


     }
    }

     ?>

Trucker Account Model truckeraccount_model.php

      <?php
       class Truckeraccount_model extends CI_Model{

       public function __construct() {
        parent::__construct();
       }

        function get_all()
    {
        $this->db->select('l_address','d_address','dom');    
        $q = $this->db->get('consignment');
        return $q->result_array();

      }

     }
     ?>

Solution

  • In your 'truckerlogin_ctrl' you have this redirect:

    redirect('truckeraccount_view');
    

    But you haven't any controller named truckeraccount_view if you want to set a view, use the load->view() instead or create the controller..

    In the view part truckerlogin_view.php, you haven't set the form tag:

    <form action='url' name='form' ... > << here you link the view with the controller

    You only have the submit button (if you don't implement with JS it wouldn't work).