Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. <?php
  2. // Initialize the session
  3. session_start();
  4.  
  5. // Check if the user is already logged in, if yes then redirect him to welcome page
  6. if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
  7. header("location: welcome.php");
  8. exit;
  9. }
  10.  
  11. // Include config file
  12. require_once "db-config.php";
  13.  
  14. // Define variables and initialize with empty values
  15. $username = $password = "";
  16. $username_err = $password_err = "";
  17.  
  18. // Processing form data when form is submitted
  19. if($_SERVER["REQUEST_METHOD"] == "POST"){
  20. // Check if username is empty
  21. if(empty(trim($_POST["username"]))){
  22. $username_err = "Please enter username.";
  23. } else{
  24. $username = trim($_POST["username"]);
  25. }
  26. // Check if password is empty
  27. if(empty(trim($_POST["password"]))){
  28. $password_err = "Please enter your password.";
  29. } else{
  30. $password = trim($_POST["password"]);
  31. }
  32. // Validate credentials
  33. if(empty($username_err) && empty($password_err)){
  34. // Prepare a select statement
  35. $sql = "SELECT id, user_name, user_password FROM lib_user WHERE user_name = ?";
  36.  
  37. if($stmt = mysqli_prepare($conn, $sql)){
  38. // Bind variables to the prepared statement as parameters
  39. mysqli_stmt_bind_param($stmt, 's', $param_username);
  40. // Set parameters
  41. $param_username = $username;
  42. // Attempt to execute the prepared statement
  43. if(mysqli_stmt_execute($stmt)){
  44. // Store result
  45. mysqli_stmt_store_result($stmt);
  46. // Check if username exists, if yes then verify password
  47. if(mysqli_stmt_num_rows($stmt) == 1){
  48. // Bind result variables
  49. mysqli_stmt_bind_result($stmt, $id, $user_name, $user_password);
  50. if(mysqli_stmt_fetch($stmt)){
  51. if(password_verify($password, $user_password)){
  52. // Password is correct, so start a new session
  53. session_start();
  54. // Store data in session variables
  55. $_SESSION["loggedin"] = true;
  56. $_SESSION["id"] = $id;
  57. $_SESSION["username"] = $user_name;
  58.  
  59. // Redirect user to welcome page
  60. header("location: welcome.php");
  61. } else{
  62. // Display an error message if password is not valid
  63. // $password_err = "The password you entered was not valid.";
  64. $password_err = "$user_password";
  65. }
  66. }
  67. } else{
  68. // Display an error message if username doesn't exist
  69. $username_err = "No account found with that username.";
  70. }
  71. } else{
  72. echo "Oops! Something went wrong. Please try again later.";
  73. }
  74. // Close statement
  75. mysqli_stmt_close($stmt);
  76. }
  77. }
  78.  
  79. // Close connection
  80. mysqli_close($conn);
  81. }
  82. ?>
  83.  
  84. <!DOCTYPE html>
  85. <html lang="en">
  86. <head>
  87. <meta charset="UTF-8">
  88. <title>Login</title>
  89. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  90. <style type="text/css">
  91. body{ font: 14px sans-serif; }
  92. .wrapper{ width: 350px; padding: 20px; }
  93. </style>
  94. </head>
  95. <body>
  96. <div class="wrapper">
  97. <h2>Login</h2>
  98. <p>Please fill in your credentials to login.</p>
  99. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  100. <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  101. <label>Username</label>
  102. <input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
  103. <span class="help-block"><?php echo $username_err; ?></span>
  104. </div>
  105. <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  106. <label>Password</label>
  107. <input type="password" name="password" class="form-control">
  108. <span class="help-block"><?php echo $password_err; ?></span>
  109. </div>
  110. <div class="form-group">
  111. <input type="submit" class="btn btn-primary" value="Login">
  112. </div>
  113. <p>Don't have an account? <a href="register.php">Sign up now</a>.</p>
  114. </form>
  115. </div>
  116. </body>
  117. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement