Advertisement
Guest User

Untitled

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