CodeIgniter Forums

Full Version: Session Validation before come to controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need Valid is user login form enter below company controller. Please help me to do this session validate. Company controller and login model are below here. 
Controller
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Company extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
       $this->load->model("Login_model");
   }

    public function index()
    {  
        $this->load->view('header');
        $this->load->view('top_header');
        $this->load->view('left_nav');
        $this->load->view('company/index');
        $this->load->view('footer');
        $this->load->view('settings');
    }


    //Add New Company
    public function company_create()
    {

           $this->load->view('includes/header');
           $this->load->view('includes/top_header');
           $this->load->view('includes/left_nav');
           $this->load->view('company/create');
           $this->load->view('includes/footer');
           $this->load->view('includes/settings');


    }

    //Save create company data to DB
    public function save()
    {
       $this->load->helper(array('form', 'url'));
       $this->load->library('form_validation');

       $this->form_validation->set_rules('company_name','Company Name','required|max_length[500]');


       if($this->form_validation->run() == TRUE) {
           $this->load->model('Company_model');
           $this->Company_model->insert();
           redirect('company/all_company');
       }
       else {

           redirect('company/company_create');
       }


    }

    //View Added Company List
    public function all_company()
    {

        $this->load->model('Company_model');
        $data["result"] = $this->Company_model->all_company();

        $this->load->view('includes/header');
        $this->load->view('includes/top_header');
        $this->load->view('includes/left_nav');
        $this->load->view('company/all', $data);
        $this->load->view('includes/footer');
        $this->load->view('includes/settings');
    }

    //View Individual Company Data
    public function view_company($id)
    {    
        $this->load->model('Company_model');
        $data["row"] = $this->Company_model->view_company_data($id);
            

            if ($data["row"] ==null) {
               $this->load->view('includes/header');
               $this->load->view('includes/top_header');
               $this->load->view('includes/left_nav');
               $this->load->view('error_page/404');
               $this->load->view('includes/footer');
               $this->load->view('includes/settings');
            }else{
                $this->load->view('includes/header');
               $this->load->view('includes/top_header');
               $this->load->view('includes/left_nav');
               $this->load->view('company/view', $data);
               $this->load->view('includes/footer');
               $this->load->view('includes/settings');
            }
       
    }

    //Get Data to form to edit data
    public function company_update($id)
    {
        $this->load->model('Company_model');
        $data["company"] = $this->Company_model->get($id);

       if ($data["company"] ==null) {
           $this->load->view('includes/header');
           $this->load->view('includes/top_header');
           $this->load->view('includes/left_nav');
           $this->load->view('error_page/404');
           $this->load->view('includes/footer');
           $this->load->view('includes/settings');
       }
       else {
           $this->load->view('includes/header');
           $this->load->view('includes/top_header');
           $this->load->view('includes/left_nav');
           $this->load->view('company/update', $data);
           $this->load->view('includes/footer');
           $this->load->view('includes/settings');
       }

    }

    //Update create company data to DB
    public function update($id)
    {
        $this->load->model('Company_model');
        $this->Company_model->update($id);
        redirect('company/all_company');
    }

    //Block unwanted Company
    public function company_delete($id)
    {
        $setstatus=array('status' => 1);
        $wherestatus=array('id' => $id);

        $this->load->model('Company_model');
        $this->Company_model->delete('company',$setstatus, $wherestatus);

        redirect("company/all_company");
    }

    //Show blocked company
    public function show_suspended_companies()
    {

        $this->load->model('Company_model');
        $data["result"] = $this->Company_model->get_suspended_companies();

       $this->load->view('includes/header');
       $this->load->view('includes/top_header');
       $this->load->view('includes/left_nav');
       $this->load->view('company/suspended', $data);
       $this->load->view('includes/footer');
       $this->load->view('includes/settings');
    }

    //Unblock the suspended companies
    public function company_un_delete($id)
    {    
        $wherestatus=array('status' => 0);
        $setstatus=array('id' => $id);

        $this->load->model('Company_model');
        $this->Company_model->un_delete('company', $wherestatus, $setstatus);

        redirect("company/all_company");
    }

    public function delete_company_from_db($id)
   {
       $this->load->model('Company_model');
       $data = $this->Company_model->delete_company($id);
           redirect("company/all_company");

   }

    
}

Login Model

Code:
<?php
Class Login_model extends CI_model
{
   public function __construct()
   {
       parent:: __construct();
       $this->load->library('session');
   }

   public function user_create()
   {
       $data["user_name"] = $this->input->post('user_name');
       $data["email"] = $this->input->post('email');
       $data["password"] = $this->input->post('password');
       $data["role"] = $this->input->post('role');
       $data["status"] = 0;
       $this->db->insert('app_user', $data);

   }

   public function auth()
   {
       $email = $this->input->post('email');
       $password = md5($this->input->post('password'));


       $query = $this->db->query("SELECT * FROM app_user WHERE email='$email' AND password='$password'");

       if($query->num_rows() > 0)
       {
           $row = $query->row();
           $this->session->set_userdata('NAME', $row->user_name);
           $this->session->set_userdata('ID', $row->id);
           //$this->session->set_userdata('PHOTO', $row->image);
           return true;
       } else {
           return false;
       }
   }

   public function login_desable()
   {
       $this->session->sess_destroy();
       //$this->auth();
       //unset($_SESSION['NAME']);
   }
}
I have try this app in another hosting account. its work fine. Any one can help me please
You would do the check login in the Company Controller's constructor.
A method we are using in our project is we created a library called authlibrary. We loaded it in autoload. Then we are calling a check function in the constructor of the function. that is it.
$this->authlib->check_user();

on the constructor of the controller.
A better method would be called restrict()

Which would check the user and then do the restriction on them.

PHP Code:
    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
    
public function __construct()
    {
        
parent::__construct();

        
// restrict this controller to admins only
        
$this->auth->restrict('Admin');

        
// Load the user model - gets a lists of users
        
$this->load->model($this->models."UserModel"'users');

        
log_message('debug'"Users Controller Class Initialized");
    } 

The restrict method would check the users groups to make sure that it was the Admin.