Guest User

Untitled

a guest
Nov 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. <?php
  2. //signin.php
  3. session_start();
  4. include 'connect.php';
  5. include 'index.htm';
  6.  
  7. echo '<h3>Sign in</h3><br />';
  8.  
  9. //first, check if the user is already signed in. If that is the case, there is no need to display this page
  10. if(isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true)
  11. {
  12. echo 'You are already signed in, you can <a href="signout.php">sign out</a> if you want.';
  13. }
  14. else
  15. {
  16. if($_SERVER['REQUEST_METHOD'] != 'POST')
  17. {
  18. /*the form hasn't been posted yet, display it
  19. note that the action="" will cause the form to post to the same page it is on */
  20. echo '<form method="post" action="">
  21. Username: <input type="text" name="username" /><br />
  22. Password: <input type="password" name="password"><br />
  23. <input type="submit" value="Sign in" />
  24. </form>';
  25. }
  26. else
  27. {
  28. /* so, the form has been posted, we'll process the data in three steps:
  29. 1. Check the data
  30. 2. Let the user refill the wrong fields (if necessary)
  31. 3. Varify if the data is correct and return the correct response
  32. */
  33. $errors = array(); /* declare the array for later use */
  34.  
  35. if(!isset($_POST['username']))
  36. {
  37. $errors[] = 'The username field must not be empty.';
  38. }
  39.  
  40. if(!isset($_POST['password']))
  41. {
  42. $errors[] = 'The password field must not be empty.';
  43. }
  44.  
  45. if(!empty($errors)) /*check for an empty array, if there are errors, they're in this array (note the ! operator)*/
  46. {
  47. echo 'Uh-oh.. a couple of fields are not filled in correctly..<br /><br />';
  48. echo '<ul>';
  49. foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */
  50. {
  51. echo '<li>' . $value . '</li>'; /* this generates a nice error list */
  52. }
  53. echo '</ul>';
  54. }
  55. else
  56. {
  57. //the form has been posted without errors, so save it
  58. //notice the use of mysql_real_escape_string, keep everything safe!
  59. //also notice the sha1 function which hashes the password
  60. $sql = "SELECT
  61. id,
  62. username,
  63. user_level
  64. FROM
  65. users
  66. WHERE
  67. username = '" . mysqli_real_escape_string($link, $_POST['username']) . "'
  68. AND
  69. password = '" . sha1($_POST['password']) . "'";
  70.  
  71. $result = mysqli_query($link, $sql);
  72. if(!$result)
  73. {
  74. //something went wrong, display the error
  75. echo 'Something went wrong while signing in. Please try again later.';
  76. echo mysqli_error($link); //debugging purposes, uncomment when needed
  77. }
  78. else
  79. {
  80. //the query was successfully executed, there are 2 possibilities
  81. //1. the query returned data, the user can be signed in
  82. //2. the query returned an empty result set, the credentials were wrong
  83. if(mysqli_num_rows($result) == 0)
  84. {
  85. echo 'You have supplied a wrong user/password combination. Please try again.';
  86. }
  87. else
  88. {
  89. //set the $_SESSION['signed_in'] variable to TRUE
  90. $_SESSION['signed_in'] = true;
  91.  
  92. //we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages
  93. while($row = mysqli_fetch_assoc($result))
  94. {
  95. $_SESSION['id'] = $row['id'];
  96. $_SESSION['username'] = $row['username'];
  97. $_SESSION['user_level'] = $row['user_level'];
  98. }
  99.  
  100. echo '<META HTTP-EQUIV="Refresh" Content="0; URL=index.php">';
  101. }
  102. }
  103. }
  104. }
  105. }
  106.  
  107. ?>
Add Comment
Please, Sign In to add comment