Controlador Padrão <?php defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index(){
$this->load->helper(array('form'));
$this->load->view('login2');
}
}
?>
View do login Simple Login with CodeIgniter
Simple Login with CodeIgniter
NOME USUARIO:Senha:
Modelo da Classe class LoginModel extends CI_Model {
# VALIDA USUÁRIO
function login($loginUsuario, $senhaUsuario){
$this -> db -> select('loginUsuario, senhaUsuario, nomeUsuario');
$this -> db -> from('usuarios');
$this -> db -> where('loginUsuario', $loginUsuario);
//$this -> db -> where('password', MD5($password));
$this -> db -> where('senhaUsuario',$senhaUsuario);
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1){
return $query->result();
}
else{
return false;
}
}
# VERIFICA SE O USUÁRIO ESTÁ LOGADO
function logged() {
$logged = $this->session->userdata('logged');
if (!isset($logged) || $logged != true) {
echo 'Voce nao tem permissao para entrar nessa pagina. Efetuar Login';
die();
}
}
}
Verificar Login
**
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Description: Classe para Verificar login do sistema
* @author Alex José Silva
* @package login
*/
class verificarLogin extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('LoginModel','',TRUE);
}
function index(){
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('loginUsuario', 'loginUsuario', 'trim|required');
$this->form_validation->set_rules('senhaUsuario', 'senhaUsuario', 'trim|required|callback_check_database');
if($this->form_validation->run() == FALSE){
//Field validation failed. User redirected to login page
$this->load->view('login2');
}
else{
//Go to private area
redirect('home', 'refresh');
}
}
function check_database($senhaUsuario){
//Field validation succeeded. Validate against database
$loginUsuario = $this->input->post('loginUsuario');
//query the database
$result = $this->LoginModel->login($loginUsuario, $senhaUsuario);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'nomeUsuario' => $row->nomeUsuario
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
**
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description: Classe home apos lógin
*
* @author
*/
session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller {
function index()
{
if($this->session->userdata('logged_in')){
$session_data = $this->session->userdata('logged_in');
$data['loginUsuario'] = $session_data['loginUsuario'];
$this->load->view('home', $data);
}
else{
//If no session, redirect to login page
redirect('login', 'refresh');
}
//corpo da pagia
$this->load->view('elementos/cabecalho',$dados);
$this->load->view('elementos/menuSuperior',$dados);
$this->load->view('elementos/menuInferior',$dados);
$this->load->view('elementos/conteudo',$dados);
$this->load->view('elementos/rodape',$dados);
}
//saida do sistema
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}