Guest User

Untitled

a guest
Aug 17th, 2016
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. // Updated, correct version of the login function
  2. function login($username, $password, $mysqli) {
  3.  
  4. date_default_timezone_set('Europe/Bucharest');
  5.  
  6. // Using prepared statements means that SQL injection is not possible.
  7. if ($stmt = $mysqli->prepare("SELECT user_id, password, salt, privilages
  8. FROM users
  9. WHERE username = '$username'")) {
  10.  
  11. // Execute the prepared query.
  12. $stmt->execute();
  13. $stmt->store_result();
  14.  
  15. // Get variables from result.
  16. $stmt->bind_result($user_id, $db_password, $salt, $privilage);
  17. $stmt->fetch();
  18.  
  19. // Hash the password with the unique salt.
  20. $password = trim($password);
  21. $password = hash('sha512', $password . $salt);
  22.  
  23. if ($stmt->num_rows == 1) {
  24.  
  25. // TODO
  26. // If the user exists we check if the account is locked
  27. // from too many login attempts
  28. if (checkbrute($user_id, $mysqli) == true) {
  29. // Account is locked
  30. // Send an email to user saying their account is locked
  31. return false;
  32. } else {
  33. // Check if the password in the database matches
  34. // the password the user submitted.
  35. if ($db_password == $password) {
  36.  
  37. $date = date('Y-m-d H:i:s', time());
  38. $ip = $_SERVER['REMOTE_ADDR'];
  39.  
  40. $result = $mysqli->query("SELECT ip
  41. FROM login
  42. WHERE username = '".$username."'");
  43.  
  44. // If that user is not logged in already, log him
  45. if ($result->num_rows == 0) {
  46. $mysqli->query("INSERT INTO login(ip, username, date)
  47. VALUES ('$ip', '$username', '$date')");
  48.  
  49. // If the user is already logged on another pc, update the ip
  50. } else {
  51. $mysqli->query("UPDATE login SET ip='$ip', date='$date' WHERE username='".$username."'");
  52. }
  53.  
  54. // Login successful.
  55. return true;
  56. } else {
  57. // Password is not correct
  58. // We record this attempt in the database
  59. $now = date('H:i:s', time());
  60. //$mysqli->query("INSERT INTO login_attempts(user_id, time)
  61. // VALUES ('$user_id', '$now')");
  62. return false;
  63. }
  64. }
  65. } else {
  66. // No user exists.
  67. return false;
  68. }
  69. }
  70. }
Add Comment
Please, Sign In to add comment