Advertisement
Guest User

Untitled

a guest
May 9th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 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.  
  18. // Processing form data when form is submitted
  19. if($_SERVER["REQUEST_METHOD"] == "POST"){
  20.  
  21. // Check if username is empty
  22. if(empty(trim($_POST["username"]))){
  23. $username_err = "Please enter username.";
  24. } else{
  25. $username = trim($_POST["username"]);
  26. }
  27.  
  28. // Check if password is empty
  29. if(empty(trim($_POST["password"]))){
  30. $password_err = "Please enter your password.";
  31. } else{
  32. $password = trim($_POST["password"]);
  33. }
  34.  
  35. // Validate credentials
  36. if(empty($username_err) && empty($password_err)){
  37. // Prepare a select statement
  38. $sql = "SELECT id, username, password FROM users WHERE username = ?";
  39.  
  40. if($stmt = mysqli_prepare($link, $sql)){
  41. // Bind variables to the prepared statement as parameters
  42. mysqli_stmt_bind_param($stmt, "s", $param_username);
  43.  
  44. // Set parameters
  45. $param_username = $username;
  46.  
  47. // Attempt to execute the prepared statement
  48. if(mysqli_stmt_execute($stmt)){
  49. // Store result
  50. mysqli_stmt_store_result($stmt);
  51.  
  52. // Check if username exists, if yes then verify password
  53. if(mysqli_stmt_num_rows($stmt) == 1){
  54. // Bind result variables
  55. mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
  56. if(mysqli_stmt_fetch($stmt)){
  57. if(password_verify($password, $hashed_password)){
  58. // Password is correct, so start a new session
  59. session_start();
  60.  
  61. // Store data in session variables
  62. $_SESSION["loggedin"] = true;
  63. $_SESSION["id"] = $id;
  64. $_SESSION["username"] = $username;
  65.  
  66. // Redirect user to welcome page
  67. header("location: welcome.php");
  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.  
  82. // Close statement
  83. mysqli_stmt_close($stmt);
  84. }
  85.  
  86. // Close connection
  87. mysqli_close($link);
  88. }
  89. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement