Advertisement
Guest User

Untitled

a guest
Oct 30th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 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. //http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined
  23. $postdata = file_get_contents("php://input");
  24. if (isset($postdata)) {
  25. $request = json_decode($postdata);
  26. $username = $request->username;
  27. $password = $request->password;
  28.  
  29. if ($username == "arhamzul@gmail.com" && $password == "pass1234") {
  30. $status = "pass";
  31. $message = "Server returns: " . $username;
  32. } else {
  33. $status = "fail";
  34. $message = 'Invalid username or password';
  35. }
  36. } else {
  37. $status = "fail";
  38. $message = "Empty input";
  39. }
  40.  
  41. $data = [
  42. 'status' => $status,
  43. 'message' => $message,
  44. ];
  45.  
  46. echo json_encode($data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement