Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. <?php
  2. require_once 'User.php';
  3. require_once 'DB.php';
  4.  
  5. class UserTools {
  6.  
  7. //Log the user in. First checks to see if the
  8. //username and password match a row in the database.
  9. //If it is successful, set the session variables
  10. //and store the user object within.
  11. public function login($username, $password)
  12. {
  13.  
  14. $hashedPassword = md5($password);
  15.  
  16. $result = mysql_query('SELECT * FROM "users" WHERE username = "'.$username.'" AND password = "'.$hashedPassword.'"');
  17.  
  18. if(mysql_num_rows($result) == 1)
  19. {
  20. $_SESSION["user"] = serialize(new User(mysql_fetch_assoc($result)));
  21. $_SESSION["login_time"] = time();
  22. $_SESSION["logged_in"] = 1;
  23. return true;
  24. }else{
  25. echo mysql_error();
  26. return false;
  27. }
  28. }
  29.  
  30. //Log the user out. Destroy the session variables.
  31. public function logout() {
  32. unset($_SESSION['user']);
  33. unset($_SESSION['login_time']);
  34. unset($_SESSION['logged_in']);
  35. session_destroy();
  36. }
  37.  
  38. //Check to see if a username exists.
  39. //This is called during registration to make sure all user names are unique.
  40. public function checkUsernameExists($username) {
  41. $result = mysql_query("select id from users where username='$username'");
  42. if(mysql_num_rows($result) == 0)
  43. {
  44. return false;
  45. }else{
  46. return true;
  47. }
  48. }
  49.  
  50. //get a user
  51. //returns a User object. Takes the users id as an input
  52. public function get($id)
  53. {
  54. $db = new DB();
  55. $result = $db->select('users', "id = $id");
  56.  
  57. return new User($result);
  58. }
  59.  
  60. }
  61. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement