Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if($_SERVER['REQUEST_METHOD'] === 'POST') {
- if(isset($_POST['is_login'])) {
- $username = $_POST['username'];
- $password = $_POST['password'];
- $errors = [];
- $success = false;
- if(empty($username)) {
- $errors[] = 'Please enter a username.';
- }
- if(empty($password)) {
- $errors[] = 'Please enter your password.';
- }
- if(count($errors) == 0) {
- if ($username != 'paul123' || $password != '123123') {
- $errors[] = 'Incorrect username or password.';
- } else {
- $success = true;
- }
- }
- $error_class = count($errors) > 0 ? 'alert-danger' : 'alert-success';
- $errors_alert = '<div class="alert ' . $error_class . '">';
- foreach ($errors as $error) {
- $errors_alert .= $error . '<br>';
- }
- if($success) {
- $errors_alert .= "Welcome back paul123, you are being redirected!";
- }
- $errors_alert .= '</div>';
- header('Content-Type: application/json');
- echo json_encode([
- 'error' => !empty($errors),
- 'errors_alert' => $errors_alert,
- ]);
- }
- exit;
- }
- ?>
- <html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport"
- content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Document</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
- </head>
- <body>
- <div class="container">
- <div class="row">
- <div class="col-md-6">
- <button class="btn btn-primary" data-toggle="modal" data-target="#login-modal">Login</button>
- </div>
- </div>
- </div>
- <div class="modal fade" id="login-modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class="modal-header">
- <h5 class="modal-title" id="exampleModalLabel">Login</h5>
- <button type="button" class="close" data-dismiss="modal" aria-label="Close">
- <span aria-hidden="true">×</span>
- </button>
- </div>
- <form action="" method="post" id="login-form">
- <div class="modal-body">
- <div class="help-block"></div>
- <div class="form-group">
- <label for="username">Username</label>
- <input type="text" class="form-control" name="username" id="username"/>
- </div>
- <div class="form-group">
- <label for="password">Password</label>
- <input type="password" class="form-control" name="password" id="password">
- </div>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
- <button type="submit" class="btn btn-primary">Login</button>
- </div>
- </form>
- </div>
- </div>
- </div>
- <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
- <script>
- $(document).ready(function () {
- $('form#login-form').on('submit', function (e) {
- e.preventDefault();
- const form = $(this);
- const username = $(this).find('input#username').val();
- const password = $(this).find('input#password').val();
- $.ajax({
- url: '/',
- method: 'post',
- dataType: 'json',
- data: { is_login: 1, username: username, password: password },
- success: function (response) {
- form.find('div.help-block').html(response.errors_alert);
- if(!response.error) {
- setTimeout(function () {
- window.location.href = 'http://facebook.com/';
- }, 2000);
- }
- }
- });
- });
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement