Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. <?php
  2.  
  3. class Auth extends CI_Controller {
  4.    
  5.     function __construct()
  6.     {
  7.         parent::__construct();
  8.     }
  9.    
  10.     function index()
  11.     {
  12.        
  13.     }
  14.    
  15.     function login()
  16.     {
  17.         $this->load->model('auth_model');
  18.        
  19.         $username = $this->input->post('username');
  20.         $password = $this->input->post('password');
  21.        
  22.         if ($this->auth_model->validate_login($username, $password))
  23.         {
  24.             // login success
  25.        
  26.             $this->session->set_userdata('logged_in', TRUE);
  27.            
  28.             if ($this->auth_model->validate_admin($username))
  29.             {
  30.                 $this->session->set_userdata('is_admin', TRUE);
  31.             }
  32.             else
  33.             {
  34.                 $this->session->set_userdata('is_admin', FALSE);
  35.             }
  36.            
  37.             $session_id = $this->session->userdata('session_id');
  38.             if ($this->session->userdata('logged_in'))
  39.             {
  40.                 echo 'Logged in!';
  41.                 echo "<br>";
  42.                 echo $session_id;
  43.             }
  44.         }
  45.         else
  46.         {
  47.             // login failed
  48.             echo 'Login failed';
  49.         }
  50.     }
  51.    
  52.     function logout()
  53.     {
  54.         $this->session->sess_destroy();
  55.        
  56.         redirect('home');
  57.     }
  58. }
  59.  
  60.  
  61.  
  62. ?>
  63.  
  64. // auth_model.php ===========================================
  65.  
  66. <?php
  67.  
  68. class Auth_model extends CI_Model {
  69.    
  70.     function __construct()
  71.     {
  72.         parent::__construct();
  73.     }
  74.    
  75.     function index()
  76.     {
  77.        
  78.     }
  79.    
  80.     function validate_login($username, $password)
  81.     {  
  82.         $this->db->where('username', $username);
  83.         $this->db->where('password', md5($password));
  84.         $result = $this->db->get('users');
  85.        
  86.         if ($result->num_rows() == 1)
  87.         {
  88.             return TRUE;
  89.         }
  90.         else
  91.         {
  92.             return FALSE;
  93.         }
  94.     }
  95.    
  96.     function validate_admin($username)
  97.     {  
  98.         $this->db->where('username', $username);
  99.         $this->db->where('admin', 1);
  100.         $result = $this->db->get('users');
  101.        
  102.         if ($result->num_rows() == 1)
  103.         {
  104.             return TRUE;
  105.         }
  106.         else
  107.         {
  108.             return FALSE;
  109.         }
  110.     }
  111. }
  112.  
  113. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement