Guest User

Untitled

a guest
Mar 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. <?php
  2. Auth::connect($config);
  3.  
  4. class Auth
  5. {
  6. public static $config = NULL;
  7. public static $dbh = NULL;
  8. public static $stmt = NULL;
  9. public static $username = NULL;
  10. public static $password = NULL;
  11. public static $sessdata = NULL;
  12. public static $dbuser = NULL;
  13. public static $dbuser = NULL;
  14. public static $database = NULL;
  15. public static $host = NULL;
  16.  
  17. public static function configure($config = NULL) {
  18. self::$dbuser = self::$config["username"];
  19. self::$dbpass = self::$config["password"];
  20. self::$database = self::$config["database"];
  21. self::$host = self::$config["host"];
  22. }
  23.  
  24. public static function connect($config = NULL) {
  25. if(is_array($config)) {
  26. self::config($config);
  27. }
  28. self::$dbh = new PDO("mysql:host={self::$host};dbname={self::$database}", self::$dbuser, self::$dbpass);
  29. self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  30. }
  31.  
  32. public static function validateUser() {
  33. self::$stmt = self::$dbh->prepare("SELECT * FROM users WHERE username = :username AND password = :password LIMIT 1");
  34. self::$stmt->bindParam(':username', self::$username);
  35. self::$stmt->bindParam(':password', md5(self::$password));
  36. self::$stmt->execute();
  37.  
  38. return self::$stmt->rowCount() == 1;
  39. }
  40.  
  41. public static function isAuth() {
  42. if(self::$sessdata["ip"] != $_SERVER['REMOTE_ADDR']) {
  43. return false;
  44. }
  45. self::$stmt = self::$dbh->prepare("SELECT * FROM users WHERE id = :id AND password = :password LIMIT 1");
  46. self::$stmt->bindParam(':id', self::$sessdata["id"]);
  47. self::$stmt->bindParam(':password', self::$sessdata["salty"]);
  48. self::$stmt->execute();
  49.  
  50. return self::$stmt->rowCount() == 1;
  51. }
  52. public static function generateSess() {
  53. $result = self::$stmt->fetch();
  54. return array("id" => $result["id"], "ip" => $_SERVER["REMOTE_ADDR"], "salty" => $result["password"]);
  55. }
  56. }
  57. ?>
Add Comment
Please, Sign In to add comment