Add authentication to the staging website using codeigniter php

advertisements

Hi i need to add authentication to my staging website index page.Whenever the user tries to open the staging site it should show login page.Once user login then only he can see the website.I have tried added a code for displaying login page but it is displaying blank page not displaying any error messages as well.Here is my code. As my website looks link will be for eg:staging.website.com whenever the user tries to open this site it should ask login first.

Controller:

class Welcome extends CI_Controller {
function __construct()
        {
            parent::__construct();
    $this->load->library('form_validation');
    $this->load->model('index_model');
    $this->load->model('login_model');
        }
public function index()
{
    if($this->session->userdata('admin_logged_in'))
    {
        $data['admin_details'] = $this->session->userdata('admin_logged_in');
        $data['records2'] = $this->index_model->get_all_banners();
        $data['records7'] = $this->index_model->get_all_banners();
        $data['mainpage'] = "index";
        $this->load->view('templates/templatessss',$data);
    }
    else
    {
        $this->load->view('login');
    }

}

public function login(){
    $this->form_validation->set_rules('user_name','User Name','trim|required|xss_clean');
    $this->form_validation->set_rules('password','Password','trim|required|xss_clean');
    $this->form_validation->set_error_delimiters('<span class="ferror"> ','</span><br />');
    if($this->form_validation->run()==FALSE){
        $this->load->view('login');
    }
    else{
        $user_name = $this->input->post('user_name');
        $password = $this->input->post('password');
        if($this->login_model->login_user($user_name,$password)):
            redirect('welcome');
        else:
            $data['error'] = '<span class="ferror"> User Name or Password enter wrong. Please try again </span>';
            $this->load->view('login',$data);
        endif;
    }
}

view(login.php):

<?php

$form_attributes  = array('class'=>'admin_login_form', 'name'=>'admin_login_form', 'id'=>'admin_login_form', 'enctype' => "multipart/form-data");
$user_name       = array('name' => 'user_name', 'id' => 'user_name', 'value' => set_value('user_name'),'class' =>'text');
$password = array('name' => 'password', 'id' => 'password','class' =>'text');
$submit = array('id' => 'login_details', 'value' => 'Login', 'name' => 'login_details');
?>

            <?php echo form_open('welcome/login',$form_attributes);?>
            <?php echo (validation_errors()) ? validation_errors() : ' ';?>
            <?php echo (isset($error)) ? $error : '' ;?>
                <?php echo form_label('User Name', $user_name['id']);?>
                <?php echo form_input($user_name); if(!$this->input->post('user_name')) echo form_error('$user_name');?>
                <?php echo form_label('Password', $password['id']);?>
                <?php echo form_password($password);?>
                <div class="sep"></div>
                <button type="submit" class="ok"><?php echo form_submit($submit);?></button>

Model(login_model.php):

class login_model extends MY_Model
{
function login_user($user_name = '', $password=''){
    $userdetails = array(
    'user_name' => $user_name,
    'password' => md5($password),
    );
    $this->db->where($userdetails);
    $query = $this->db->get('login_details');
    if($query->num_rows()):
        $user = $query->result();
        $sess_arry = array(
            'user_id' => $user[0]->user_id,
            'name' => $user[0]->name
        );
        $this->session->set_userdata('admin_logged_in', $sess_arry);
   return true;
    else:
        return false;
    endif;
}
}


Add this code in your Controller:

class Welcome extends CI_Controller {
  function __construct()
  {
     parent::__construct();
     $this->load->library('form_validation');
     $this->load->model('index_model');
     $this->load->model('login_model');
  }
   public function index()
   {
     if($this->session->userdata('admin_logged_in'))
     {
       $data['admin_details'] = $this->session->userdata('admin_logged_in'); 

      $data['records2'] = $this->index_model->get_all_banners();
      $data['records7'] = $this->index_model->get_all_banners();
       $data['mainpage'] = "index";
       $this->load->view('templates/templatessss',$data);
    }
    else
    {
         redirect('welcome/login');
    }

I hope it will work for you!!