Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. <?php
  2. require_once( 'db-connect.php' );
  3. session_name( 'a' ); // The session name must be one word
  4. session_start();
  5.  
  6. if( isset( $_SESSION['id'] ) && isset( $_SESSION['name'] ) ) {
  7. $id = $_SESSION['id'];
  8. $name = $_SESSION['name'];
  9. $logged_in = true;
  10. }
  11. else {
  12. $logged_in = false;
  13. }
  14. $username = $_POST['username'];
  15. $password = $_POST['password'];
  16.  
  17. print '<p>Attempting to login user '.$username.'...';
  18.  
  19. // Prepare a SELECT statement to get the user details from the DB
  20. $statement = mysqli_prepare( $link, "SELECT id, name, pass
  21. FROM authors
  22. WHERE name=?");
  23. if( $statement ) {
  24. // Bind in the supplied username
  25. mysqli_stmt_bind_param( $statement, 's', $username );
  26. // Run the statement
  27. mysqli_stmt_execute( $statement );
  28. // Supply empty variables for the user's record data
  29. mysqli_stmt_bind_result( $statement, $id ,$name, $pass);
  30.  
  31. print '<p>Checking for matching user...';
  32.  
  33. // Get the matching record (if any)
  34. if( mysqli_stmt_fetch( $statement ) ) {
  35.  
  36. print '<p>User account found. Checking password...';
  37. if( ( $password) == $pass )
  38. // Password hashes match. Store the session information
  39. $_SESSION["id"] = $id;
  40. $_SESSION["name"] = $username;
  41.  
  42. // Head back to the home page after letting the user know
  43. print '<p>Password is correct… Success!';
  44. header( 'refresh:2;url=index.php' );
  45. }
  46. else {
  47. // Incorrect password... try again
  48. print '<p class="error">Incorrect password!';
  49. header( 'refresh:20;url=form-login.php' );
  50. }
  51. }
  52. else {
  53. // No user account exists. Invalid login... try again
  54. print '<p class="error">Unknown user!';
  55. header( 'refresh:2;url=form-login.php' );
  56. }
  57.  
  58. // Close the prepared statement.
  59. mysqli_stmt_close( $statement );
  60.  
  61.  
  62. // Close the database connection
  63. mysqli_close( $link );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement