Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. // preparing a temporary table that unions the admin and employee tables
  2. $TMP = "SELECT EMP_ID AS ID, EMP_EMAIL AS EMAIL, 2 AS TYPE FROM employee
  3. UNION
  4. SELECT ID, EMAIL, 1 AS TYPE FROM admin ";
  5. $result = $con->prepare($TMP);
  6. $result-> execute();
  7. $result-> store_result();
  8. $result-> fetch();
  9.  
  10. // preparing select statement for logging in
  11. $stmt = $con->prepare('SELECT `TYPE` FROM `".$result."` WHERE `ID` = ?');
  12. // Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
  13. $stmt->bind_param('i', $_POST['ID']);
  14. $stmt->execute();
  15. // Store the result so we can check if the account exists in the database.
  16. $stmt->store_result();
  17. if ($stmt->num_rows > 0) {
  18. $stmt->bind_result($ID);
  19. $stmt->fetch();
  20. }
  21. if ($stmt['TYPE'] == 1) {
  22. if ($_POST['ID'] == $ID) {
  23. // Verification success! User has loggedin!
  24. // Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
  25. session_regenerate_id();
  26. $_SESSION['loggedin'] = TRUE;
  27. $_SESSION['email'] = $_POST['EMAIL'];
  28. $_SESSION['id'] = $ID;
  29. echo 'Welcome ' . $_SESSION['email'] . '!';
  30. } else {
  31. echo 'Incorrect password!';
  32. }
  33. } else if ($stmt['TYPE'] == 2) {
  34. if ($_POST['ID'] == $ID) {
  35. // Verification success! User has loggedin!
  36. // Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
  37. session_regenerate_id();
  38. $_SESSION['loggedin'] = TRUE;
  39. $_SESSION['email'] = $_POST['EMAIL'];
  40. $_SESSION['id'] = $ID;
  41. echo 'Welcome ' . $_SESSION['email'] . '!';
  42. } else {
  43. echo 'Incorrect password!';
  44. }
  45. }
  46. $stmt-> close();
  47.  
  48.  
  49.  
  50. $result-> close();
  51. $con-> close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement