Advertisement
Mujiburrohman

Login/Register API

Apr 12th, 2019
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.65 KB | None | 0 0
  1. function login(){
  2.  
  3.  $username = $this->input->post("username");
  4.  $password = $this->input->post("password");
  5.  
  6.  $this->db->where("username",$username);
  7.  $this->db->where("password",md5($password));
  8.  
  9.  
  10.  $hasil = $this->db->get("tb_user");
  11.  
  12.   //check query ada datanya apa enggak
  13.   if($hasil -> num_rows() > 0 ){
  14.  
  15.     //bikin response k mobile
  16.    $data['pesan'] = "login berhasil";
  17.    $data['sukses']  = true ;
  18.    $data['login'] = $hasil->row();
  19.  
  20.   }
  21.   else{
  22.    $data['pesan'] = "username atau password salah";
  23.    $data['sukses']  = false ;
  24.  
  25.   }
  26.   echo json_encode($data);
  27.  
  28. }
  29. function register() {
  30.         // Variabel untuk menampung data dari user/mobile/postman
  31.         $nama = $this->input->post('nama');
  32.         $username = $this->input->post('username');
  33.         $email = $this->input->post('email');
  34.         $pass = $this->input->post('password');
  35.  
  36.         // Ambil data email dari tb_biodata
  37.         $this->db->where('email', $email);
  38.         $check_email = $this->db->get('tb_user');
  39.  
  40.         // Cek email apakah sudah ada di database
  41.         if($check_email -> num_rows() > 0) {
  42.             $response['status'] = false;
  43.             $response['pesan'] = "Email sudah terdaftar";
  44.         } else {
  45.             // Insert data ke table
  46.             $insert = array();
  47.             $insert['nama'] = $nama;
  48.             $insert['username'] = $username;
  49.             $insert['email'] = $email;
  50.             $insert['password'] = md5($pass);
  51.  
  52.             $simpan = $this->db->insert('tb_user', $insert);
  53.  
  54.             if($simpan) {
  55.                 $response['status'] = true;
  56.                 $response['pesan'] = "Register berhasil";
  57.             }
  58.             else {
  59.                 $response['status'] = false;
  60.                 $response['pesan'] = "Register gagal, silahkan cek koneksi internet";
  61.             }
  62.         }
  63.  
  64.         echo json_encode($response);
  65.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement