Advertisement
anggagewor

Untitled

Dec 15th, 2021
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.90 KB | None | 0 0
  1. <?php
  2. header('Content-Type: application/json; charset=utf-8');
  3. $connect = '';
  4. function checkUser($method)
  5. {
  6.     // global $connect;
  7.     $response = [
  8.         'status' => 0,
  9.         'message' => ''
  10.     ];
  11.     $headers = getallheaders();
  12.     // https://stackoverflow.com/questions/53610622/what-does-double-question-mark-operator-mean-in-php
  13.     $type   = $headers['Type'] ?? null;
  14.     $id     = $headers["Device"] ?? null;
  15.  
  16.     // jika $type atau $id == null
  17.     // return false
  18.     if (!$type || !$id) {
  19.         $response['message'] = 'id and type can not be blank';
  20.         return json_encode($response);
  21.     }
  22.  
  23.     // jika $type ==mercu
  24.     if ($type == 'Mercu') {
  25.         $headers["User-Agent"] == $headers["Device"];
  26.         $datas = null; // inisiasi karena data line 24 blm di define
  27.         $sql = "SELECT * FROM member where Device = $id";
  28.         // $result = $connect->query($sql);
  29.         // while ($row = mysqli_fetch_object($result)) {
  30.         for ($x = 1; $x <= 10; $x++) {
  31.             $row = new stdClass;
  32.             $row->xxx = 'qwerty ' . $x;
  33.             $datas[] = [
  34.                 'Mercu' => $row->xxx,
  35.                 'Admin' => $row->xxx,
  36.                 'Expired' => $row->xxx,
  37.                 'Message' => $row->xxx,
  38.             ];
  39.         }
  40.         // }
  41.         // $data = ['ok'];
  42.         if (!$datas) {
  43.             $response['message'] = 'No data to display';
  44.             return json_encode($response);
  45.         }
  46.         // anggap lah column di member isi nya
  47.         // id,name,expired_date,created_date
  48.         // [{"Mercu":"12345","Admin":"@KopiNih","Expired":"15-12-2021","Message":"Mercu"}]}
  49.         $response['status'] = 1;
  50.         $response['data'] = $datas;
  51.         return json_encode($response);
  52.     }
  53.  
  54.     // return false
  55.     // jika $type != mercu
  56.     $response['message'] = 'invalid type';
  57.     return json_encode($response);
  58. }
  59.  
  60. echo checkUser('get');
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement