Advertisement
21ani

Controller Auth2

Dec 14th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.18 KB | None | 0 0
  1. <?php
  2. class Auth extends CI_Controller {
  3.  
  4.     public function __construct(){
  5.         parent::__construct();
  6.         $this->load->library('form_validation');
  7.     }
  8.  
  9.     public function index(){
  10.         $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
  11.         $this->form_validation->set_rules('password', 'Password', 'trim|required');
  12.  
  13.         if ($this->form_validation->run() == false){
  14.             $data['judul'] = 'Halaman Login';
  15.         $this->load->view('login/vheader', $data);
  16.         $this->load->view('login/visi');
  17.         $this->load->view('login/vfooter');
  18.         }
  19.         else{
  20.             //ketika validasi lolos atau sukses
  21.             $this->_login();
  22.         }
  23.     }
  24.  
  25.     private function _login(){
  26.         $email = $this->input->post('email'); //ngambil email yang ada di elemen email
  27.         $password = $this->input->post('password'); //ngambil password yang ada di elemen email
  28.  
  29.         //$user = $this->db->get_where('user',['email=> $email'])->row_array();
  30.         $user = $this->db->get_where('user',['email'=> $email])->row_array();
  31.         // var_dump($user);
  32.         // die;
  33.         if($user){
  34.             if ($user['is_active'] == 1){
  35.                 if (password_verify($password,$user['password'])){
  36.                     $data =[
  37.                         'email'=> $user['email'],
  38.                         'role_id'=> $user['role_id']
  39.                     ];
  40.                     $this->session->set_userdata($data);
  41.                     redirect('user');
  42.  
  43.                 } else {
  44.                     $this->session->set_flashdata('massage','<div class="alert alert-danger" role="alert">
  45.                Wrong Password
  46.              </div>');
  47.                 //setelah database berhasil masuk, kita  redirect ke halaman index auth.
  48.                 redirect('auth');
  49.  
  50.                 }
  51.  
  52.             } else{
  53.                 $this->session->set_flashdata('massage','<div class="alert alert-danger" role="alert">
  54.                This Email has not been activated!
  55.              </div>');
  56.                 //setelah database berhasil masuk, kita  redirect ke halaman index auth.
  57.                 redirect('auth');
  58.  
  59.             }
  60.  
  61.         } else{
  62.             $this->session->set_flashdata('massage','<div class="alert alert-danger" role="alert">
  63.            Email is not registered!
  64.          </div>');
  65.             //setelah database berhasil masuk, kita  redirect ke halaman index auth.
  66.             redirect('auth');
  67.  
  68.         }
  69.     }
  70.    
  71.     public function registration(){
  72.  
  73.         //aturan untuk kolom inputan pada form
  74.  
  75.         $this->form_validation->set_rules('name', 'Name', 'required|trim');
  76.         $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]',[
  77.             'is_unique' => 'This Email has already registered'
  78.         ]);
  79.         $this->form_validation->set_rules('password1', 'Password', 'required|trim|
  80. min_length[3]|matches[password2]',[
  81.     'min_lenght' => 'Password too short!',
  82.     'matches' => 'Password dont matches'
  83. ]);
  84.         $this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
  85.  
  86.  
  87.         //logika untuk mengembalikan tampilan apabila form_validasi nya gagal
  88.         if ($this->form_validation->run() == false ){
  89.             $this->load->helper('url');
  90.  
  91.             $data['judul'] = 'Halaman Login';
  92.  
  93.         $this->load->view('login/vheader', $data);
  94.         $this->load->view('login/vregistration');
  95.         $this->load->view('login/vfooter');
  96.         } else {
  97.             //membuat sebuah variabel utk isian perintah memasukkan apa yang diinputkan di form input
  98.             //lalu memasukkan di tabel database
  99.             $data = [
  100.                 'name' => htmlspecialchars($this->input->post('name',true)),
  101.                 'email' => htmlspecialchars($this->input->post('email',true)),
  102.                 'image' => 'default.jpg',
  103.                 //'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
  104.                 'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
  105.                 'role_id' => 2,
  106.                 'is_active' => 1,
  107.                 'date_created' => time()
  108.             ];
  109.  
  110.             //memasukkan variabel tadi ke tabel user
  111.             $this->db->insert('user',$data);
  112.  
  113.             //Kasih pesan sebelum redirect Buat flashdata pakai session
  114.             $this->session->set_flashdata('massage','<div class="alert alert-success" role="alert">
  115.            Congrats your account has been ceated!
  116.          </div>');
  117.             //setelah database berhasil masuk, kita  redirect ke halaman index auth.
  118.             redirect('auth');
  119.         }
  120.        
  121.     }
  122.  
  123.     public function logout(){
  124.         $this->session->unset_userdata('email');
  125.         $this->session->unset_userdata('role_id');
  126.         //Kasih pesan sebelum redirect Buat flashdata pakai session
  127.         $this->session->set_flashdata('massage','<div class="alert alert-success" role="alert">
  128.        You have been logged out!
  129.      </div>');
  130.         //setelah database berhasil masuk, kita  redirect ke halaman index auth.
  131.         redirect('auth');
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement