Advertisement
Guest User

Untitled

a guest
Dec 8th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1.  
  2. if($_SERVER['REQUEST_METHOD'] === 'POST') {
  3. if(isset($_POST['is_login'])) {
  4. $username = $_POST['username'];
  5. $password = $_POST['password'];
  6. $errors = [];
  7. $success = false;
  8.  
  9. if(empty($username)) {
  10. $errors[] = 'Please enter a username.';
  11. }
  12. if(empty($password)) {
  13. $errors[] = 'Please enter your password.';
  14. }
  15.  
  16. if(count($errors) == 0) {
  17. if ($username != 'paul123' || $password != '123123') {
  18. $errors[] = 'Incorrect username or password.';
  19. } else {
  20. $success = true;
  21. }
  22. }
  23.  
  24. $error_class = count($errors) > 0 ? 'alert-danger' : 'alert-success';
  25. $errors_alert = '<div class="alert ' . $error_class . '">';
  26. foreach ($errors as $error) {
  27. $errors_alert .= $error . '<br>';
  28. }
  29. if($success) {
  30. $errors_alert .= "Welcome back paul123, you are being redirected!";
  31. }
  32. $errors_alert .= '</div>';
  33.  
  34. header('Content-Type: application/json');
  35. echo json_encode([
  36. 'error' => !empty($errors),
  37. 'errors_alert' => $errors_alert,
  38. ]);
  39. }
  40.  
  41. exit;
  42. }
  43.  
  44. ?>
  45.  
  46. <html>
  47. <head>
  48. <meta charset="UTF-8">
  49. <meta name="viewport"
  50. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  51. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  52. <title>Document</title>
  53.  
  54. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
  55. </head>
  56. <body>
  57.  
  58. <div class="container">
  59. <div class="row">
  60. <div class="col-md-6">
  61. <button class="btn btn-primary" data-toggle="modal" data-target="#login-modal">Login</button>
  62. </div>
  63. </div>
  64. </div>
  65.  
  66. <div class="modal fade" id="login-modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  67. <div class="modal-dialog" role="document">
  68. <div class="modal-content">
  69. <div class="modal-header">
  70. <h5 class="modal-title" id="exampleModalLabel">Login</h5>
  71. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  72. <span aria-hidden="true">&times;</span>
  73. </button>
  74. </div>
  75. <form action="" method="post" id="login-form">
  76. <div class="modal-body">
  77. <div class="help-block"></div>
  78. <div class="form-group">
  79. <label for="username">Username</label>
  80. <input type="text" class="form-control" name="username" id="username"/>
  81. </div>
  82. <div class="form-group">
  83. <label for="password">Password</label>
  84. <input type="password" class="form-control" name="password" id="password">
  85. </div>
  86. </div>
  87. <div class="modal-footer">
  88. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  89. <button type="submit" class="btn btn-primary">Login</button>
  90. </div>
  91. </form>
  92. </div>
  93. </div>
  94. </div>
  95.  
  96. <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  97. <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
  98. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
  99.  
  100. <script>
  101. $(document).ready(function () {
  102. $('form#login-form').on('submit', function (e) {
  103. e.preventDefault();
  104. const form = $(this);
  105. const username = $(this).find('input#username').val();
  106. const password = $(this).find('input#password').val();
  107. $.ajax({
  108. url: '/',
  109. method: 'post',
  110. dataType: 'json',
  111. data: { is_login: 1, username: username, password: password },
  112. success: function (response) {
  113. form.find('div.help-block').html(response.errors_alert);
  114. if(!response.error) {
  115. setTimeout(function () {
  116. window.location.href = 'http://facebook.com/';
  117. }, 2000);
  118. }
  119. }
  120. });
  121. });
  122. });
  123. </script>
  124. </body>
  125. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement