Advertisement
eskimopest

login

Dec 29th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.04 KB | None | 0 0
  1. <?php
  2. session_set_cookie_params(0, '/', 'localhost', false, true);
  3. session_start();
  4. // create form token
  5. $_SESSION['token_login'] = bin2hex(random_bytes(32));
  6. ?>
  7. <!-- THE FORM -->
  8. <form class="login" method="post" name="login" id="login-form">
  9.     <input type="text" class="form-control" name="username" id="username" placeholder="Username">
  10.     <input type="password" class="form-control " name="password" id="password" placeholder="Password">
  11.     <input type="hidden" name="token" id="token" value="<?php echo $_SESSION['token_login']; ?>">
  12.     <input type="text" name="user" id="user" class="hidden" />
  13.     <a href="javascript:void(0)" onclick="sendLogin()" class="btn btn-success btn-block">Entrar</a>
  14. </form>
  15. <div class="msgs"></div>
  16.  
  17. <!-- the ajax call -->
  18. <script>
  19. function sendLogin() {
  20.     var msgs = $('.msgs');
  21.  
  22.     var data = $('#login-form').serialize();
  23.     var url = 'ajax/login.ajax.php';
  24.  
  25.     $.ajax({
  26.         url: url,
  27.         data: data,
  28.         type: 'POST',
  29.         success: function(response) {
  30.             var json = $.parseJSON(response);
  31.  
  32.             $('input').removeClass('is-invalid');
  33.  
  34.             var fields = json.fields;
  35.             for(var i=0; i<fields.length; i++) {
  36.                 $('#'+fields[i]).addClass('is-invalid');
  37.             }
  38.  
  39.             msgs.html('<p class="error-msgs">'+json.msg+'</p>');
  40.  
  41.             $('#token').val(json.token);
  42.  
  43.             if(json.type == 'success') {
  44.                 msgs.html('<p class="success">Redirecting...</p>');
  45.                 $(location).attr("href","dashboard.php");
  46.             }
  47.         }
  48.     });
  49. }
  50. </script>
  51.  
  52.  
  53.  
  54. <?php
  55.     // THE LOGIN.AJAX.PHP PAGE
  56.     session_start();
  57.  
  58.     if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST)) {
  59.         $username = clearTags($_POST['username']);
  60.         $password = clearTags($_POST['password']);
  61.         $token = clearTags($_POST['token']);
  62.         $user = clearTags($_POST['user']);
  63.  
  64.         $error = false;
  65.         $msg = '';
  66.         $fields = [];
  67.         $type = '';
  68.  
  69.         if(strlen($username) < 3) {
  70.             $error = true;
  71.             array_push($fields, 'username');
  72.         }
  73.         if(strlen($password) < 3) {
  74.             $error = true;
  75.             array_push($fields, 'password');
  76.         }
  77.         if($token !== $_SESSION['token_login']) {
  78.             $error = true;
  79.             array_push($fields, 'token');
  80.         }
  81.         if(strlen($user) !== 0) {
  82.             $error = true;
  83.             array_push($fields, 'user');
  84.         }
  85.  
  86.         if($error) {
  87.             $msg = 'Existem erros no formul&aacute;rio!';
  88.             $type = 'error';
  89.         }
  90.         else {
  91.  
  92.         }
  93.         // generate new token to send to form
  94.         $_SESSION['token_login'] = bin2hex(random_bytes(32));
  95.         // prepare response to send as json
  96.         $response = [];
  97.         $response['type'] = $type;
  98.         $response['fields'] = $fields;
  99.         $response['msg'] = $msg;
  100.         $response['token'] = $_SESSION['token_login'];
  101.  
  102.         echo json_encode($response);
  103.     }
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement