Advertisement
Guest User

Untitled

a guest
Feb 11th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. <?php
  2. $serverName = "localhost"; //Variables to access the user database
  3. $username = "root";
  4. $password = "";
  5. $database = "snake_database";
  6. $errors = []; //Array of all the errors to display to the user
  7.  
  8. $conn = mysqli_connect($serverName, $username, $password, $database); //Connect to the database
  9.  
  10. if(!$conn){ //If the database failed to connect
  11.  
  12. die("Database failed to connect: " .mysqli_connect_error()); //Display an error message
  13. }
  14.  
  15. $username = $_POST['username']; //set the username/ password varaibles
  16. $password = $_POST['password'];
  17. $hashPass = password_hash($password, PASSWORD_DEFAULT); //Encrypt the password
  18.  
  19. $sql = "SELECT * FROM users WHERE username = ?"; //Select all usernames and passwords
  20. $stmt = $conn->prepare($sql);
  21. $stmt->bind_param("s", $username);
  22. $stmt->execute();
  23. $result = $stmt->get_result();
  24.  
  25. if (password_verify($password, $userRow['password']))
  26. {
  27. $count = 1;
  28. }
  29. else
  30. {
  31. $count = 0;
  32. }
  33.  
  34. if($count == 1) //If there is 1 account that matches
  35. {
  36. $stmt->close(); //Close the statment and connection
  37. $conn->close();
  38.  
  39. session_start();
  40. $_SESSION["LoggedUser"] = $username; //Log the user in
  41. $_SESSION["lastPage"] = "login.php";
  42. header("location: profile.php"); //Direct the user to their profile
  43.  
  44. }else //if there is no accounts that match
  45. {
  46. array_push($errors, "Username or password is incorrect");
  47. session_start();
  48. $_SESSION["loginErrors"] = $errors;
  49. $_SESSION["lastPage"] = "login.php"; //Make this page the last page
  50. header("location: index.php"); //Go to the homepage
  51. }
  52. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement