Advertisement
Guest User

Untitled

a guest
Aug 17th, 2016
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. function login($username, $password, $mysqli) {
  2. echo $username;
  3. echo '<br>';
  4. echo $password;
  5. // Using prepared statements means that SQL injection is not possible.
  6. if ($stmt = $mysqli->prepare("SELECT user_id, password, privilages
  7. FROM users
  8. WHERE username = '$username'")) {
  9. //$stmt->bind_param('s', $username); // Bind "$email" to parameter.
  10. $stmt->execute(); // Execute the prepared query.
  11. $stmt->store_result();
  12.  
  13. // get variables from result.
  14. $stmt->bind_result($user_id, $db_password, $privilages);
  15. $stmt->fetch();
  16.  
  17. if ($stmt->num_rows == 1) {
  18. // Check if the password in the database matches
  19. // the password the user submitted.
  20. if ($db_password == $password) {
  21. // Password is correct!
  22. // Get the user-agent string of the user.
  23. $user_browser = $_SERVER['HTTP_USER_AGENT'];
  24. // XSS protection as we might print this value
  25. $user_id = preg_replace("/[^0-9]+/", "", $user_id);
  26. $_SESSION['user_id'] = $user_id;
  27. // XSS protection as we might print this value
  28. $username = preg_replace("/[^a-zA-Z0-9_\-]+/",
  29. "",
  30. $username);
  31. $_SESSION['username'] = $username;
  32. $_SESSION['privilages'] = $privilages;
  33. $_SESSION['login_string'] = hash('sha512',
  34. $password . $user_browser);
  35. // Login successful.
  36. return true;
  37. } else {
  38. // Password is not correct
  39. // We record this attempt in the database
  40. $now = time();
  41. return false;
  42. }
  43. } else {
  44. // No user exists.
  45. return false;
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement