Guest User

Untitled

a guest
Jan 5th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. enter code here <?php require_once 'connect.php';
  2.  
  3. // Check if username is empty
  4. if(empty(trim($_POST["username"]))){
  5. $username_err = 'Please enter username.';
  6. } else{
  7. $username = trim($_POST["username"]);
  8. }
  9.  
  10. // Check if password is empty
  11. if(empty(trim($_POST['password']))){
  12. $password_err = 'Please enter your password.';
  13. } else{
  14. $password = trim($_POST['password']);
  15. }
  16.  
  17. // Validate credentials
  18. if(empty($username_err) && empty($password_err)){
  19. // Prepare a select statement
  20. $sql = "SELECT username,password FROM users WHERE username = '".$_POST['username']."'";
  21.  
  22. if($stmt = mysqli_prepare($link, $sql)){
  23. // Bind variables to the prepared statement as parameters
  24.  
  25. // Set parameters
  26. $param_username = $username;
  27. //$param_password = password_hash($password, PASSWORD_DEFAULT);
  28.  
  29. // Attempt to execute the prepared statement
  30. if(mysqli_stmt_execute($stmt)){
  31. // Store result
  32. mysqli_stmt_store_result($stmt);
  33.  
  34. // Check if username exists, if yes then verify password
  35. if(mysqli_stmt_num_rows($stmt) == 1){
  36. // Bind result variables
  37. mysqli_stmt_bind_result($stmt, $username, $hashed_password);
  38. if(mysqli_stmt_fetch($stmt)){
  39. if(password_verify($password, $hashed_password)){
  40. /* Password is correct, so start a new session and
  41. save the username to the session */
  42. session_start();
  43. $_SESSION['username'] = $username;
  44. header("location: welcome.php");
  45. } else{
  46. // Display an error message if password is not valid
  47. $password_err = 'The password you entered was not valid.';
  48. }
  49. }
  50. } else{
  51. // Display an error message if username doesn't exist
  52. $username_err = 'No account found with that username.';
  53. }
  54. } else{
  55. echo "Oops! Something went wrong. Please try again later.";
  56. }
  57. }
  58.  
  59. // Close statement
  60. //mysqli_stmt_close($stmt);
  61. }
  62.  
  63. // Close connection
  64. mysqli_close($link);
Add Comment
Please, Sign In to add comment