Advertisement
Guest User

Untitled

a guest
Jul 17th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. $(document).ready(function () {
  2.  
  3. // jQuery validate plugin
  4. $('#login > form').validate({
  5.  
  6. rules: {
  7. username: { required: true },
  8. password: { required: true }
  9. },
  10.  
  11. submitHandler: function () {
  12.  
  13. var credentials = {
  14. "username": $('#username').val(),
  15. "password": $('#password').val()
  16. };
  17.  
  18. $.post('ajax/tryLogin.php', credentials)
  19. .done(function (data) {
  20.  
  21. if (data.hasOwnProperty('success') &&
  22. data['success']) {
  23.  
  24. window.location = 'catalog.php';
  25.  
  26. } else if (data.hasOwnProperty('message')) {
  27.  
  28. alert(data['message']);
  29. } else {
  30. alert('Communication with the server failed.');
  31. }
  32. })
  33. .fail(function () {
  34. alert('Communication with the server failed.');
  35. })
  36. }
  37. });
  38. });
  39.  
  40. if (empty($_POST['username']) || empty($_POST['password'])) {
  41. $data['success'] = false;
  42. $data['message'] = 'Username and password are required.';
  43.  
  44. } else {
  45.  
  46. try {
  47. $data['success'] = Security::TryLogin($_POST['username'], $_POST['password']);
  48. } catch (Exception $e) {
  49.  
  50. $data['success'] = false;
  51. $data['message'] = $e->getMessage();
  52. }
  53. }
  54. header('Content-type: application/json');
  55. echo json_encode($data);
  56.  
  57. class Security
  58. {
  59.  
  60. //...
  61.  
  62. public static function TryLogin($username, $password)
  63. {
  64. $username = strtolower($username);
  65. $password = sha1($password . $username);
  66.  
  67. try {
  68. $user = Users::FindByUsernameAndPassword($username, $password);
  69.  
  70. if (session_id() == '') {
  71. session_start();
  72. }
  73.  
  74. $_SESSION[self::USER_IDENTIFIER] = $user;
  75.  
  76. return true;
  77.  
  78. } catch (Exception $e) {
  79. throw new Exception('Username of password incorrect.');
  80. }
  81. }
  82.  
  83. //...
  84.  
  85. }
  86.  
  87. <?php
  88.  
  89. include_once('config.php');
  90. include_once(ROOT . 'libs/database.php');
  91. include_once(ROOT . 'libs/models/user.php');
  92.  
  93. class Security
  94. {
  95.  
  96. //...
  97.  
  98. public static function TryLogin($username, $password)
  99. {
  100. $username = strtolower($username);
  101. $password = sha1($password . $username);
  102.  
  103. $conn = Database::getConnection();
  104. if (empty($conn)) {
  105. throw new Exception('The connection to the database failed.');
  106. }
  107.  
  108. $result = odbc_exec($conn, '{CALL [BruPartsOrderDb].[dbo].[tryLogin]("' . $username . '", "' . $password . '")}');
  109. if (empty($result)) {
  110. throw new Exception('The execution of the query failed.');
  111. }
  112.  
  113. $row = odbc_fetch_row($result);
  114. if (empty($row)) {
  115. throw new Exception('Username or password incorrect.');
  116. }
  117.  
  118. if (session_id() == '') {
  119. session_start();
  120. }
  121.  
  122. $_SESSION['user'] = new User(odbc_result($result, 'id'), $username);
  123.  
  124. return true;
  125. }
  126.  
  127. //...
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement