Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. <?php
  2.  
  3. if(isset($_POST['submit'])){
  4. session_start();
  5. // configuration
  6. $dbhost = "localhost";
  7. $dbname = "login";
  8. $dbuser = ".";
  9. $dbpass = ".";
  10.  
  11. // database connection
  12. $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
  13. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  14. $conn->exec("SET CHARACTER SET utf8mb4");
  15. // new data
  16.  
  17. $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
  18. $passwordAttempt = !empty($_POST['password']) ? trim($_POST['password']) : null;
  19.  
  20. // query
  21. $result = "SELECT id, username, password, banned FROM users WHERE username= :username AND banned = '0'";
  22.  
  23. $stmt = $conn->prepare($result);
  24. $stmt->bindValue(':username', $username);
  25. $stmt->execute();
  26. $user = $stmt->fetch(PDO::FETCH_ASSOC);
  27.  
  28. if($user === false){
  29. //Could not find a user with that username!
  30. //PS: You might want to handle this error in a more user-friendly manner!
  31. $error = "Incorrect username/password combination!";
  32. } else{
  33. //User account found. Check to see if the given password matches the
  34. //password hash that we stored in our users table.
  35.  
  36. //Compare the passwords.
  37. $validPassword = password_verify($passwordAttempt, $user['password']);
  38.  
  39. //If $validPassword is TRUE, the login has been successful.
  40. if($validPassword){
  41.  
  42. $_SESSION['login_user'] = $username;
  43. header("location: home.php");
  44. exit;
  45.  
  46. } else{
  47. $error = "Incorrect username/password combination!";
  48. }
  49. }
  50.  
  51. }
  52.  
  53. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement