Advertisement
wahyufruixer

masuk.php

Oct 25th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.75 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL ^ E_DEPRECATED);
  3. /**
  4.  * File to handle all API requests
  5.  * Accepts GET and POST
  6.  *
  7.  * Each request will be identified by TAG
  8.  * Response will be JSON data
  9.  
  10.   /**
  11.  * check for POST request
  12.  */
  13. if (isset($_POST['tag']) && $_POST['tag'] != '') {
  14.     // get tag
  15.     $tag = $_POST['tag'];
  16.  
  17.     // include DB_function
  18.     require_once 'DB_Functions.php';
  19.     $db = new DB_Functions();
  20.  
  21.     // response Array
  22.     $response = array("tag" => $tag, "error" => FALSE);
  23.  
  24.     // checking tag
  25.     if ($tag == 'login') {
  26.         // Request type is check Login
  27.         $email = $_POST['email'];
  28.         $password = $_POST['password'];
  29.  
  30.         // check for user
  31.         $user = $db->getUserByEmailAndPassword($email, $password);
  32.  
  33.         if ($user != false) {
  34.             // user found
  35.             $response["error"] = FALSE;
  36.             $response["user_id"] = $user["NIMHSMSMHS"];
  37.             $response["user"]["name"] = $user["KDPSTMSMHS"];
  38.             $response["user"]["email"] = $user["NIMHSMSMHS"];
  39.             echo json_encode($response);
  40.         } else {
  41.             // user not found
  42.             // echo json with error = 1
  43.             $response["error"] = TRUE;
  44.             $response["user_id"] = null;
  45.             $response["error_msg"] = "Incorrect email or password!";
  46.             echo json_encode($response);
  47.         }
  48.     } else if ($tag == 'register') {
  49.         // Request type is Register new user
  50.         $name = $_POST['name'];
  51.         $email = $_POST['email'];
  52.         $password = $_POST['password'];
  53.  
  54.         // check if user is already existed
  55.         if ($db->isUserExisted($email)) {
  56.             // user is already existed - error response
  57.             $response["error"] = TRUE;
  58.             $response["error_msg"] = "User already existed";
  59.             echo json_encode($response);
  60.         } else {
  61.             // store user
  62.             $user = $db->storeUser($name, $email, $password);
  63.             if ($user) {
  64.                 // user stored successfully
  65.                 $response["error"] = FALSE;
  66.                 $response["uid"] = $user["user_id"];
  67.                 $response["user"]["name"] = $user["user_name"];
  68.                 $response["user"]["email"] = $user["user_email"];
  69.                 echo json_encode($response);
  70.             } else {
  71.                 // user failed to store
  72.                 $response["error"] = TRUE;
  73.                 $response["error_msg"] = "Error occured in Registartion";
  74.                 echo json_encode($response);
  75.             }
  76.         }
  77.     } else {
  78.         // user failed to store
  79.         $response["error"] = TRUE;
  80.         $response["error_msg"] = "Unknown 'tag' value. It should be either 'login' or 'register'";
  81.         echo json_encode($response);
  82.     }
  83. }
  84. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement