web-dev-qa-db-fra.com

Système de connexion Codeigniter avec session pour rediriger l'utilisateur vers la page si le mot de passe est correct

J'ai créé un système de connexion, mais chaque fois que je configure une instruction if, il est renvoyé à la page de connexion lorsque je saisis le mot de passe correct. J'ai besoin de la fonction index dans le contrôleur, des fonctions list_employee et View_employee pour rediriger les utilisateurs vers la page de connexion s'ils y accèdent directement, mais s'ils entrent un mot de passe correct, autorisez-les à y accéder.

contrôleur utilisateur_authentification

<?php

session_start(); //we need to start session in order to access it through CI

Class User_Authentication extends CI_Controller {

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

// Load form helper library
$this->load->helper('form');

// Load form validation library
$this->load->library('form_validation');

// Load session library
$this->load->library('session');

// Load database
$this->load->model('login_database');

}

// Show login page
public function user_login_show() {
$this->load->view('login_form');
}

// Show registration page
public function user_registration_show() {
$this->load->view('registration_form');
}

// Validate and store registration data in database
public function new_user_registration() {

// Check validation for user input in SignUp form
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('email_value', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('registration_form');
} else {
$data = array(
'name' => $this->input->post('name'),
'user_name' => $this->input->post('username'),
'user_email' => $this->input->post('email_value'),
'user_password' => $this->input->post('password')
);
$result = $this->login_database->registration_insert($data) ;
if ($result == TRUE) {
$data['message_display'] = 'Registration Successfully !';
$this->load->view('login_form', $data);
} else {
$data['message_display'] = 'Username already exist!';
$this->load->view('registration_form', $data);
}
}
}

// Check for user login process
public function user_login_process() {

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

if ($this->form_validation->run() == FALSE) {
$this->load->view('login_form');
} else {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->login_database->login($data);
if($result == TRUE){
$sess_array = array(
'username' => $this->input->post('username')
);

// Add user data in session
$this->session->set_userdata('logged_in', $sess_array);
$result = $this->login_database->read_user_information($sess_array);
if($result != false){
$data = array(
'name' =>$result[0]->name,
'username' =>$result[0]->user_name,
'email' =>$result[0]->user_email,
'password' =>$result[0]->user_password
);
redirect('employee');
}
}else{
$data = array(
'error_message' => 'Invalid Username or Password'
);
$this->load->view('login_form', $data);
}
}
}

// Logout from admin page
public function logout() {

// Removing session data
$sess_array = array(
'username' => ''
);
$this->session->unset_userdata('logged_in', $sess_array);
$data['message_display'] = 'Successfully Logout';
$this->load->view('login_form', $data);
}
}

?>

contrôleur des employés

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

class Employee extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->model('login/employee_model');

        }   

    //Shows the dashboard
    public function index()
    {

        $this->load->view('header');
        $this->load->view('employee');
        $this->load->view('login/footer');



    }
    //Insert the employee 
    public function  insert_employee()
    { 


        $data=array('name'=>$this->input->post('name'),
            'LanId'=>$this->input->post('LanId'),
            'reason'=>$this->input->post('reason'),
            'PepNumber'=>$this->input->post('PepNumber'),
            'Employee_Number'=>$this->input->post('Employee_Number'),
            'department'=>$this->input->post('department'),

            'status'=>1);
        //print_r($data);

        $result=$this->employee_model->insert_employee($data);
        if($result==true)
        {
            $this->session->set_flashdata('msg',"Employee Records Added Successfully");
            redirect('employee');

        }
        else
        {

            $this->session->set_flashdata('msg1',"Employee Records Added Failed");
            redirect('employee');


        }
    }
    //List of Employees 
        public function list_employees()
    {



            $data['employee']=$this->employee_model->get_employee();
            $this->load->view('header');
            $this->load->view('list_of_employees',$data);
             $this->load->view('login/footer');

    }
    //List of Employees 
        public function viewlist_employees()
    {


            $data['employee']=$this->employee_model->get_employee();
            $this->load->view('header');
            $this->load->view('viewlist_of_employees',$data);
             $this->load->view('login/footer');

    }

    public function delete_employee()
    {
        $id=$this->input->post('id');
        $data=array('status'=>0);
        $result=$this->employee_model->delete_employee($id,$data);
        if($result==true)
        {
            $this->session->set_flashdata('msg1',"Deleted Successfully");
            redirect('employee/list_employees');

        }
        else
        {

            $this->session->set_flashdata('msg1',"Employee Records Deletion Failed");
            redirect('employee/list_employees');


        }

    }
    public function edit_employee()
    {
        $id=$this->uri->segment(3);
        $data['employee']=$this->employee_model->edit_employee($id);
        $this->load->view('header',$data);
        $this->load->view('edit_employee');
    }
    public function  update_employee()
    {
        $id=$this->input->post('id');

        $data=array('name'=>$this->input->post('name'),
            'LanID'=>$this->input->post('LanID'),
            'reason'=>$this->input->post('reason'),
            'PepNumber'=>$this->input->post('PepNumber'),
            'Employee_Number'=>$this->input->post('Employee_Number'),
            'department'=>$this->input->post('department'),

            'status'=>1);

        $result=$this->employee_model->update_employee($data,$id);
        if($result==true)
        {
            $this->session->set_flashdata('msg',"Employee Records Updated Successfully");
            redirect('employee/list_employees');

        }
        else
        {

            $this->session->set_flashdata('msg1',"No changes Made in Employee Records");
            redirect('employee/list_employees');


        }
    }

}
?>

modèle login_database

<?php

Class Login_Database extends CI_Model {

// Insert registration data in database
public function registration_insert($data) {

// Query to check whether username already exist or not
$condition = "user_name =" . "'" . $data['user_name'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 0) {

// Query to insert data in database
$this->db->insert('user_login', $data);
if ($this->db->affected_rows() > 0) {
return true;
}
} else {
return false;
}
}

// Read data using username and password
public function login($data) {

$condition = "user_name =" . "'" . $data['username'] . "' AND " . "user_password =" . "'" . $data['password'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();

if ($query->num_rows() == 1) {
return true;
} else {
return false;
}
}

// Read data from database to show data in admin page
public function read_user_information($sess_array) {

$condition = "user_name =" . "'" . $sess_array['username'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();

if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}

}

?>

employee_model

<?php

class Employee_model extends CI_Model 
{

    public function insert_employee($data)
    {
        $this->db->insert('employee_list',$data);
        return ($this->db->affected_rows() != 1 ) ? false:true;
    }
    public function get_employee()
    {
        $this->db->select('*');
        $this->db->from('employee_list');
        $this->db->where('status',1);

        $query =$this->db->get();
        return $query->result();
    }
    public function delete_employee($id,$data)
    {
        $this->db->where('id',$id);
        $this->db->update('employee_list',$data);
        return ($this->db->affected_rows() != 1 ) ? false:true;
    }
    public function edit_employee($id)
    {
        $this->db->select('*');
        $this->db->from('employee_list');
        $this->db->where('id',$id);
        $this->db->where('status',1);
        $query =$this->db->get();
        return $query->result();

    }
    public function update_employee($data,$id)
    {
        $this->db->where('id',$id);
        $this->db->update('employee_list',$data);
        return ($this->db->affected_rows() != 1 ) ? false:true;
    }
}
4
Veronica

ajoute une instruction if avec log_in et une redirection vers le formulaire de connexion si elle est est incorrect

public function index()
        {
             if($this->session->userdata('logged_in'))
            {      
            $this->load->view('header');
            $this->load->view('employee');
            $this->load->view('login/footer');

           }else{
               redirect('user_authentication/user_login_show');

            }

        }
4
Veronica

La meilleure pratique consiste à ajouter le contrôle dans le constructeur de votre contrôleur dans CI . Voici mon exemple.

 public function __construct() {
    parent::__construct();
    if (!$this->session->userdata('user_data')) {
        return redirect('login');
    }

    $this->load->model('customer_model');
}

vous pouvez ajouter l'instruction else pour rediriger vers le tableau de bord ou la page résultante si l'utilisateur est connecté. 

2
ankit suthar

Ajoutez cette ligne de code à vos constructeurs:

$this->load->library('session');

Cela vous aidera.

0
Jay Bhatia
public function login()
{
    $this->load->view('login');

    if (isset($_POST['login'])) 
    {

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

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

        if($this->main_model->can_login('$emailid','$Password'))
        {   

            $session_data = array(
                 'emailid' => $emailid,
                 'password' => $password,
                 'iss_logged_in' => 1
            );
            $this->session->set_userdata($session_data);
             redirect(base_url().'index.php/Hello_cnt/');

        }
        else
        {
             $this->session->set_flashdata('error', 'Invalid Username and Password');
           redirect(base_url().'index.php/Hello_cnt/login');

        }
    }   
}
0
Jaydev Vara