Advertisement
21ani

Controller Auth

Dec 13th, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.27 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Auth extends CI_Controller {
  5.  
  6.     public function __construct(){
  7.         parent::__construct();
  8.         $this->load->library('form_validation');
  9.     }
  10.     public function index(){
  11.         $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
  12.         $this->form_validation->set_rules('password', 'Password', 'trim|required');
  13.  
  14.         if ($this->form_validation->run() == false){
  15.             $data['judul'] = 'Halaman Login';
  16.             $this->load->view('template/auth_header', $data);
  17.             $this->load->view('auth/login');
  18.             $this->load->view('template/auth_footer');
  19.         }
  20.         else{
  21.             //ketika validasi lolos atau sukses
  22.             $this->_login();
  23.         }
  24.     }
  25.  
  26.     private function _login(){
  27.         $email = $this->input->post('email'); //ngambil email yang ada di elemen email
  28.         $passwordd = $this->input->post('password'); //ngambil password yang ada di elemen email
  29.  
  30.         $user = $this->db->get_where('user',['email=> $email'])->row_array();
  31.         var_dump($user);
  32.         die;
  33.        
  34.     }
  35.    
  36.     public function registration(){
  37.  
  38.         //aturan untuk kolom inputan pada form
  39.  
  40.         $this->form_validation->set_rules('name', 'Name', 'required|trim');
  41.         $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]',[
  42.             'is_unique' => 'This Email has already registered'
  43.         ]);
  44.         $this->form_validation->set_rules('password1', 'Password', 'required|trim|
  45. min_length[3]|matches[password2]',[
  46.     'min_lenght' => 'Password too short!',
  47.     'matches' => 'Password dont matches'
  48. ]);
  49.         $this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
  50.  
  51.  
  52.         //logika untuk mengembalikan tampilan apabila form_validasi nya gagal
  53.         if ($this->form_validation->run() == false ){
  54.  
  55.             $data['judul'] = 'Halaman Registration';
  56.             $this->load->view('template/auth_header', $data);
  57.             $this->load->view('auth/registration');
  58.             $this->load->view('template/auth_footer');
  59.         } else {
  60.             //membuat sebuah variabel utk isian perintah memasukkan apa yang diinputkan di form input
  61.             //lalu memasukkan di tabel database
  62.             $data = [
  63.                 'name' => htmlspecialchars($this->input->post('name',true)),
  64.                 'email' => htmlspecialchars($this->input->post('email',true)),
  65.                 'image' => 'default.jpg',
  66.                 'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
  67.                 'role_id' => 2,
  68.                 'is_active' => 1,
  69.                 'date_created' => time()
  70.             ];
  71.  
  72.             //memasukkan variabel tadi ke tabel user
  73.             $this->db->insert('user',$data);
  74.  
  75.             //Kasih pesan sebelum redirect Buat flashdata pakai session
  76.             $this->session->set_flashdata('massage','<div class="alert alert-success" role="alert">
  77.            Congrats your account has been ceated!
  78.          </div>');
  79.             //setelah database berhasil masuk, kita  redirect ke halaman index auth.
  80.             redirect('auth');
  81.         }
  82.        
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement