Advertisement
Guest User

Untitled

a guest
Nov 13th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
  5. header("location: homepage.html");
  6. exit;
  7. }
  8.  
  9. require_once "config.php";
  10.  
  11. $username = $password = "";
  12. $username_err = $password_err = $login_err = "";
  13.  
  14. if($_SERVER["REQUEST_METHOD"] == "POST"){
  15.  
  16. if(empty(trim($_POST["username"]))){
  17. $username_err = "Please enter username.";
  18. } else{
  19. $username = trim($_POST["username"]);
  20. }
  21.  
  22. if(empty(trim($_POST["password"]))){
  23. $password_err = "Please enter your password.";
  24. } else{
  25. $password = trim($_POST["password"]);
  26. }
  27.  
  28. if(empty($username_err) && empty($password_err)){
  29. $sql = "SELECT ID, username, password FROM teachers WHERE username = ?";
  30.  
  31. if($stmt = mysqli_prepare($link, $sql)){
  32. mysqli_stmt_bind_param($stmt, "s", $param_username);
  33.  
  34. $param_username = $username;
  35.  
  36. if(mysqli_stmt_execute($stmt)){
  37. mysqli_stmt_store_result($stmt);
  38.  
  39. if(mysqli_stmt_num_rows($stmt) == 1){
  40. mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
  41. if(mysqli_stmt_fetch($stmt)){
  42. if(password_verify($password, $hashed_password)){
  43. session_start();
  44.  
  45. $_SESSION["loggedin"] = true;
  46. $_SESSION["id"] = $id;
  47. $_SESSION["username"] = $username;
  48.  
  49. header("location: homepage.html");
  50. } else{
  51. $login_err = "Invalid username or password.";
  52. }
  53. }
  54. } else{
  55. $login_err = "Invalid username or password.";
  56. }
  57. } else{
  58. echo "Oops! Something went wrong. Please try again later.";
  59. }
  60.  
  61. mysqli_stmt_close($stmt);
  62. }
  63. }
  64.  
  65. mysqli_close($link);
  66. }
  67. ?>
  68.  
  69.  
  70. <!DOCTYPE html>
  71. <html lang="en">
  72. <head>
  73. <meta charset="UTF-8">
  74. <title>Login</title>
  75. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  76. <style>
  77. body{ font: 14px sans-serif; }
  78. .wrapper{ width: 360px; padding: 20px; }
  79. </style>
  80. </head>
  81. <body>
  82. <div class="wrapper">
  83. <h2>Login</h2>
  84. <p>Please fill in your credentials to login.</p>
  85.  
  86. <?php
  87. if(!empty($login_err)){
  88. echo '<div class="alert alert-danger">' . $login_err . '</div>';
  89. }
  90. ?>
  91.  
  92. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  93. <div class="form-group">
  94. <label>Username</label>
  95. <input type="text" name="username" class="form-control <?php echo (!empty($username_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $username; ?>">
  96. <span class="invalid-feedback"><?php echo $username_err; ?></span>
  97. </div>
  98. <div class="form-group">
  99. <label>Password</label>
  100. <input type="password" name="password" class="form-control <?php echo (!empty($password_err)) ? 'is-invalid' : ''; ?>">
  101. <span class="invalid-feedback"><?php echo $password_err; ?></span>
  102. </div>
  103. <div class="form-group">
  104. <input type="submit" class="btn btn-primary" value="Login">
  105. </div>
  106. <p>Don't have an account? <a href="homepage.html">Sign up now</a>.</p>
  107. </form>
  108. </div>
  109. </body>
  110. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement