Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 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 "config.php";
  13.  
  14. // Define variables and initialize with empty values
  15. $username = $password = "";
  16. $username_err = $password_err = "";
  17. $balance = "";
  18.  
  19. // Processing form data when form is submitted
  20. if($_SERVER["REQUEST_METHOD"] == "POST"){
  21.  
  22. // Check if username is empty
  23. if(empty(trim($_POST["username"]))){
  24. $username_err = "Please enter username.";
  25. } else{
  26. $username = trim($_POST["username"]);
  27. }
  28.  
  29. // Check if password is empty
  30. if(empty(trim($_POST["password"]))){
  31. $password_err = "Please enter your password.";
  32. } else{
  33. $password = trim($_POST["password"]);
  34. }
  35.  
  36. // Validate credentials
  37. if(empty($username_err) && empty($password_err)){
  38. // Prepare a select statement
  39. $sql = "SELECT id, username, password FROM users WHERE username = ?";
  40.  
  41. if($stmt = mysqli_prepare($link, $sql)){
  42. // Bind variables to the prepared statement as parameters
  43. mysqli_stmt_bind_param($stmt, "s", $param_username);
  44.  
  45. // Set parameters
  46. $param_username = $username;
  47.  
  48. // Attempt to execute the prepared statement
  49. if(mysqli_stmt_execute($stmt)){
  50. // Store result
  51. mysqli_stmt_store_result($stmt);
  52.  
  53. // Check if username exists, if yes then verify password
  54. if(mysqli_stmt_num_rows($stmt) == 1){
  55. // Bind result variables
  56. mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
  57. if(mysqli_stmt_fetch($stmt)){
  58. if(password_verify($password, $hashed_password)){
  59. // Password is correct, so start a new session
  60. session_start();
  61.  
  62. }
  63. // Store data in session variables
  64. $_SESSION["loggedin"] = true;
  65. $_SESSION["id"] = $id;
  66. $_SESSION["username"] = $username;
  67. $_SESSION["balance"] = $balance;
  68. // Redirect user to welcome page
  69. header("location: welcome.php");
  70. } else{
  71. // Display an error message if password is not valid
  72. $password_err = "The password you entered was not valid.";
  73. }
  74. }
  75. } else{
  76. // Display an error message if username doesn't exist
  77. $username_err = "No account found with that username.";
  78. }
  79. } else{
  80. echo "Oops! Something went wrong. Please try again later.";
  81. }
  82. }
  83.  
  84. // Close statement
  85. mysqli_stmt_close($stmt);
  86. }
  87.  
  88. // Close connection
  89. mysqli_close($link);
  90. }
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement