Advertisement
Guest User

Untitled

a guest
Oct 30th, 2017
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. <?php
  2. header('Content-Type: application/json');
  3. //http://stackoverflow.com/questions/18382740/cors-not-working-php
  4. if (isset($_SERVER['HTTP_ORIGIN'])) {
  5. header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
  6. header('Access-Control-Allow-Credentials: true');
  7. header('Access-Control-Max-Age: 86400'); // cache for 1 day
  8. }
  9.  
  10. // Access-Control headers are received during OPTIONS requests
  11. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  12.  
  13. if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
  14. header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
  15.  
  16. if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
  17. header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
  18.  
  19. exit(0);
  20. }
  21.  
  22. // get data from mobile app
  23. $postdata = file_get_contents("php://input");
  24.  
  25. require_once("connection.php");
  26.  
  27. //http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined
  28.  
  29. if (isset($postdata)) {
  30. // convert json format to php array format
  31. $request = json_decode($postdata);
  32. $username = $request->username;
  33. $password = $request->password;
  34. // $username = 'arhamzul@gmail.com';
  35. // $password = 'pass1234';
  36. $encrypted_password = md5(sha1($password));
  37. // $encrypted_password = $password;
  38.  
  39. // put your SQL statement here
  40. $query = "SELECT * FROM users WHERE
  41. username = '$username' &&
  42. password = '$encrypted_password'";
  43.  
  44. $result = $conn->query($query);
  45. $num_rows = mysqli_num_rows($result);
  46.  
  47. if ($num_rows > 0) {
  48. $status = "pass";
  49. $message = "Server returns: " . $username;
  50.  
  51. while($row = $result->fetch_assoc()) {
  52. $name = $row["name"];
  53. $email = $row["username"];
  54. $role = $row["role"];
  55. $avatar = $row["avatar"];
  56. }
  57. } else {
  58. $status = "fail";
  59. $message = "password: $password. Encrypted: $encrypted_password";
  60. }
  61. } else {
  62. $status = "fail";
  63. $message = "Empty input";
  64. }
  65.  
  66. // bina data yang kita nak return kepada app
  67. $data = [
  68. 'status' => $status,
  69. 'message' => $message,
  70. 'name' => $name,
  71. 'role' => $role,
  72. 'avatar' => $avatar,
  73. 'email' => $email,
  74. ];
  75.  
  76. // convert data di atas kepada format json
  77. echo json_encode($data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement