Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. <?php
  2.  
  3. include("SplEnum.class.php");
  4. include("IDBConnecton.class.php");
  5.  
  6. class Enum extends SplEnum {
  7. const TUTOR = 0;
  8. const STUDENT = 1;
  9. const PARENTS = 2;
  10. }
  11.  
  12. class Authentication implements IDBConnecton {
  13.  
  14. private $db_name;
  15. private $db_user;
  16. private $db_pass;
  17. private $db_host;
  18.  
  19. private $email;
  20. private $password;
  21.  
  22. function __construct(){
  23. $this->db_name = 'the_genius';
  24. $this->db_user = 'root';
  25. $this->db_pass = '';
  26. $this->db_host = 'localhost';
  27.  
  28. // Set data into variables
  29. if(isset($_POST["email"]) == TRUE && isset($_POST["password"]) == TRUE){
  30. $this->setEmail($_POST["email"]);
  31. $this->setPassword($_POST["password"]);
  32. }
  33.  
  34. // Create connection to database and query data
  35. $this->dbConnect($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
  36. }
  37.  
  38. function setEmail($email){
  39. $this->email = $email;
  40. }
  41.  
  42. function setPassword($password){
  43. $this->password = hash('sha512', $password);
  44. }
  45.  
  46. function dbConnect($db_host, $db_user, $db_pass, $db_name) {
  47. $conn = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
  48. if ($conn->connect_error) {
  49. die("Connection failed: " . $conn->connect_error);
  50. }
  51.  
  52. $sql = "SELECT user.user_email, user.user_password, user_roles.user_roles_type
  53. FROM user
  54. INNER JOIN user_roles ON user.user_id = user_roles.user_id
  55. WHERE user.user_email = '$this->email' AND user.user_password = '$this->password'";
  56.  
  57. $query = $conn->query($sql);
  58. while($result = $query->fetch_assoc())
  59. {
  60. if ($result["user_email"] == $this->email && $result["user_password"] == $this->password) {
  61.  
  62. // CREATE NEW OBJECT AND CASTING STRING FROM user_roles_type FROM DATABASE TO INT
  63. $isRoles = new Enum((int)$result["user_roles_type"]);
  64.  
  65. // CHECK USER ROLES
  66. if ($isRoles->is(Enum::TUTOR))
  67. {
  68. echo "You are a tutor !!!";
  69. } else if ($isRoles->is(Enum::STUDENT)) {
  70. echo "You are a student !!!";
  71. } else {
  72. echo "You are a guardian or parents !!!";
  73. }
  74.  
  75. } else {
  76. }
  77. }
  78. }
  79. }
  80.  
  81. new Authentication();
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement