login.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // Define variables and initialize with empty values
  3. $username = $password = "";
  4. $username_err = $password_err = $login_err = "";
  5. // Processing form data when form is submitted
  6. if($_SERVER["REQUEST_METHOD"] == "POST"){
  7. // Check if username is empty
  8. if(empty(trim($_POST["username"]))){
  9. $username_err = "Please enter username.";
  10. } else{
  11. $username = trim($_POST["username"]);
  12. }
  13. // Check if password is empty
  14. if(empty(trim($_POST["password"]))){
  15. $password_err = "Please enter your password.";
  16. } else{
  17. $password = trim($_POST["password"]);
  18. }
  19. // Validate credentials
  20. if(empty($username_err) && empty($password_err)){
  21. // Prepare a select statement
  22. $sql = "SELECT id, username, passwd FROM users WHERE username = ? and active = 1";
  23. if($stmt = mysqli_prepare($link, $sql)){
  24. // Bind variables to the prepared statement as parameters
  25. mysqli_stmt_bind_param($stmt, "s", $param_username);
  26. // Set parameters
  27. $param_username = $username;
  28. // Attempt to execute the prepared statement
  29. if(mysqli_stmt_execute($stmt)){
  30. // Store result
  31. mysqli_stmt_store_result($stmt);
  32. // Check if username exists, if yes then verify password
  33. if(mysqli_stmt_num_rows($stmt) == 1){
  34. // Bind result variables
  35. mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
  36. if(mysqli_stmt_fetch($stmt)){
  37. if(password_verify($password, $hashed_password)){
  38. // Password is correct, so start a new session
  39. session_start();
  40. // Store data in session variables
  41. $_SESSION["loggedin"] = true;
  42. $_SESSION["id"] = $id;
  43. $_SESSION["username"] = $username;
  44. // Redirect user to welcome page
  45. header("Refresh:0");
  46. } else{
  47. // Password is not valid, display a generic error message
  48. $login_err = "Invalid username or password.";
  49. }
  50. }
  51. } else{
  52. // Username doesn't exist, display a generic error message
  53. $login_err = "Invalid username or password.";
  54. }
  55. } else{
  56. echo "Oops! Something went wrong. Please try again later.";
  57. }
  58. // Close statement
  59. mysqli_stmt_close($stmt);
  60. }
  61. }
  62. // Close connection
  63. mysqli_close($link);
  64. }
  65. ?>
  66. <!DOCTYPE html>
  67. <html lang="en">
  68. <head>
  69. <meta charset="UTF-8">
  70. <title>Connexion</title>
  71. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  72. <style>
  73. body{ font: 14px sans-serif; }
  74. .wrapper{ width: 350px; padding: 20px; }
  75. </style>
  76. </head>
  77. <body>
  78. <div class="wrapper">
  79. <img src="images/logofull.png" alt="Umbrella Logo" style="width: 100%;">
  80. <p>Entrer votre nom d'utilisateur et mot de passe pour entrer.</p>
  81. <?php
  82. if(!empty($login_err)){
  83. echo '<div class="alert alert-danger">' . $login_err . '</div>';
  84. }
  85. ?>
  86. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  87. <div class="form-group">
  88. <label>Nom d'utilisateur</label>
  89. <input type="text" name="username" class="form-control <?php echo (!empty($username_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $username; ?>">
  90. <span class="invalid-feedback"><?php echo $username_err; ?></span>
  91. </div>
  92. <div class="form-group">
  93. <label>Mot de passe</label>
  94. <input type="password" name="password" class="form-control <?php echo (!empty($password_err)) ? 'is-invalid' : ''; ?>">
  95. <span class="invalid-feedback"><?php echo $password_err; ?></span>
  96. </div>
  97. <div class="form-group">
  98. <input type="submit" class="btn btn-primary" value="Connexion">
  99. </div>
  100. </form>
  101. </div>
  102. </body>
  103. </html>