Guest User

Untitled

a guest
Apr 9th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. <?php
  2. session_start();
  3. // Checking the session
  4. if(isset($_SESSION['userSession'])){
  5. if(!empty($_SESSION['userSession'])){
  6. header("Location: models/userPanel.php");
  7. }
  8. }
  9. ?>
  10. <!DOCTYPE html>
  11. <html lang="en" dir="ltr">
  12. <head>
  13. <!-- CSS imports -->
  14. <meta name="viewport" content="width=device-width, initial-scale=1">
  15. <link rel="stylesheet" href="css/main.css" />
  16. <!-- JQuery include -->
  17. <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
  18. <!-- <script type="text/javascript" src="js/loginForm.js"></script> -->
  19. <meta charset="utf-8">
  20. <title>Login</title>
  21. </head>
  22. <body>
  23. <!-- Navigation Bar content -->
  24. <nav class="navBar">
  25. <ul>
  26. <li><a class="hover" href="index.php">Index</a></li>
  27. </ul>
  28. </nav>
  29. <!-- Body content -->
  30. <div class="bodyContainer">
  31. <!-- Form -->
  32. <div class="formContainer">
  33. <p class="formTitle">Login form</p>
  34. <form id="loginForm" method="post">
  35. <div class="formDataContainer">
  36. <div id="error"><!-- Error will be shown here ! --></div>
  37. <label for="userMail"><span class="labelText">Email</span></label>
  38. <input type="email" placeholder="Enter Username" name="userMail" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">
  39. <label for="psw"><span class="labelText">Password</span></label>
  40. <input type="password" placeholder="Enter Password" name="psw" required autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">
  41. <button type="submit" id="btnLogin" name="btnLogin">Sing in</button>
  42. <button type="button" id="registerBtn" onclick="location.href='models/registerForm.php';">Register</button>
  43. </div>
  44. </form>
  45. </div>
  46. </div>
  47. <script>
  48. $('form[id='loginForm']').on('submit', function(e){
  49. e.preventDefault();
  50. var data = $(this).serialize();
  51. $.ajax({
  52. type: 'POST', url: 'php/loginProcess.php', data: data, beforeSend: function(){
  53. $('#error').fadeOut();
  54. $('#btnLogin').html('Sending ...');
  55. }, success: function(response){
  56. if (response == 'ok'){
  57. $('#btnLogin').html('Signing In ...');
  58. setTimeout(' window.location.href = "home.php"; ', 4000);
  59. }
  60. else
  61. {
  62. $('#error').fadeIn(1000, function(){
  63. $('#error').html(response);
  64. $('#btnLogin').html('Sign In');
  65. });
  66. }
  67. },
  68. });
  69. return false;
  70. });
  71. /* login submit */
  72. </script>
  73. </body>
  74. </html>
  75.  
  76. <?php
  77. session_start();
  78. print_r($_POST);
  79. require_once 'dbConfig.php';
  80. $query = "SELECT idUser, name, surname, mail, level
  81. FROM users
  82. WHERE mail = ? AND password = ?";
  83. // TODO recoger datos
  84. if(isset($_POST["userMail"]) && isset($_POST["psw"])){
  85. if(!empty($_POST["userMail"]) && !empty($_POST["psw"])){
  86. // TODO comprobamos la base de datos
  87. // Clean obtained data
  88. $userName = $mysqli->real_escape_string(trim($_POST["userMail"]));
  89. $passwd = $mysqli->real_escape_string(trim($_POST["psw"]));
  90. // Crypt the Password
  91. $password = md5($passwd);
  92. print_r($password);
  93. // Attempt to prepare the query
  94. if($stmt = $mysqli->prepare($query)){
  95. if($stmt->bind_param("ss", $userName, $password)){
  96. $stmt->execute();
  97. // Obtain the result
  98. $result = $stmt->get_result();
  99. // If there is no result, we will show an error message
  100. if($result->num_rows === 0){
  101. echo "<script>
  102. $('#error').fadeIn(3000, function(){
  103. $('#error').html(Invalid username or password);
  104. $('#btnLogin').html('Sign In');
  105. });
  106. </script>";
  107. }else{
  108. $row = $result->fetch_array();
  109. print_r($row);
  110. }
  111. $mysqli->close();
  112. }else{
  113. // TODO handle error
  114.  
  115. }
  116. }
  117.  
  118. }
  119. }
  120. ?>
Add Comment
Please, Sign In to add comment