Advertisement
Guest User

Untitled

a guest
Jan 14th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. <?php
  2.  
  3. // Things to notice:
  4. // The main job of this script is to execute a SELECT statement to look for the submitted username and password in the appropriate database table
  5. // If the submitted username and password is found in the table, then the following session variable is set: $_SESSION["loggedInSkeleton"]=true;
  6. // All other scripts check for this session variable before loading (if it doesn't exist then the user isn't logged in and the page doesn't load)
  7. // However... the database table isn't currently being queried (at the moment the code is only checking for a username of "barryg", "mandyb" or "admin") and it's your job to add this query in...
  8. //
  9. // Other notes:
  10. // client-side validation using "password","text" inputs and "required","maxlength" attributes (but we can't rely on it happening!)
  11. // we sanitise the user's credentials - see helper.php (included via header.php) for the sanitisation function
  12. // we validate the user's credentials - see helper.php (included via header.php) for the validation functions
  13. // the validation functions all follow the same rule: return an empty string if the data is valid...
  14. // ... otherwise return a help message saying what is wrong with the data.
  15. // if validation of any field fails then we display the help messages (see previous) when re-displaying the form
  16.  
  17. // execute the header script:
  18. require_once "header.php";
  19.  
  20. // default values we show in the form:
  21. $username = "";
  22. $password = "";
  23. // strings to hold any validation error messages:
  24. $username_val = "";
  25. $password_val = "";
  26.  
  27. // should we show the signin form:
  28. $show_signin_form = false;
  29. // message to output to user:
  30. $message = "";
  31.  
  32. if (isset($_SESSION['loggedInSkeleton']))
  33. {
  34. // user is already logged in, just display a message:
  35. echo "You are already logged in, please log out first.<br>";
  36.  
  37. }
  38. elseif (isset($_POST['username']))
  39. {
  40. // user has just tried to log in:
  41.  
  42. // connect directly to our database (notice 4th argument) we need the connection for sanitisation:
  43. $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  44.  
  45. // if the connection fails, we need to know, so allow this exit:
  46. if (!$connection)
  47. {
  48. die("Connection failed: " . $mysqli_connect_error);
  49. }
  50.  
  51. // SANITISATION (see helper.php for the function definition)
  52.  
  53. // take copies of the credentials the user submitted and sanitise (clean) them:
  54. $username = sanitise($_POST['username'], $connection);
  55. $password = sanitise($_POST['password'], $connection);
  56.  
  57. // VALIDATION (see helper.php for the function definitions)
  58.  
  59. // now validate the data (both strings must be between 1 and 16 characters long):
  60. // (reasons: we don't want empty credentials, and we used VARCHAR(16) in the database table)
  61. $username_val = validateString($username, 1, 16);
  62. $password_val = validateString($password, 1, 16);
  63.  
  64. // concatenate all the validation results together ($errors will only be empty if ALL the data is valid):
  65. $errors = $username_val . $password_val;
  66.  
  67. // check that all the validation tests passed before going to the database:
  68. if ($errors == "")
  69. //Select the databases to get the data from and using variables to validate the data
  70. {
  71. $query = "SELECT * FROM members WHERE username = '$username' AND password = '$password'";
  72.  
  73. $result = mysqli_query($connection, $query);
  74. $n = mysqli_num_rows($result);
  75.  
  76.  
  77. // // currently only barryg, mandyb, or the admin can sign in... each with ANY password
  78. // // you need to replace this code with code that checks the username and password against the relevant database table...
  79. // if ($username == "barryg" || $username == "mandyb" || $username == "admin")
  80. // {
  81. // // fake a match with the database table:
  82. // $n = 1;
  83. // }
  84. // else
  85. // {
  86. // $n = 0;
  87. // }
  88. //
  89. // // if there was a match then set the session variables and display a success message:
  90. if ($n > 0)
  91. {
  92. // set a session variable to record that this user has successfully logged in:
  93. $_SESSION['loggedInSkeleton'] = true;
  94. // and copy their username into the session data for use by our other scripts:
  95. $_SESSION['username'] = $username;
  96.  
  97. // show a successful signin message:
  98. $message = "Hi, $username, you have successfully logged in, please <a href='show_profile.php'>click here</a><br>";
  99. }
  100. else
  101. {
  102. // no matching credentials found so redisplay the signin form with a failure message:
  103. $show_signin_form = true;
  104. // show an unsuccessful signin message:
  105. $message = "Sign in failed, please try again<br>";
  106. }
  107.  
  108. }
  109. else
  110. {
  111. // validation failed, show the form again with guidance:
  112. $show_signin_form = true;
  113. // show an unsuccessful signin message:
  114. $message = "Sign in failed, please check the errors shown above and try again<br>";
  115. }
  116.  
  117. // we're finished with the database, close the connection:
  118. mysqli_close($connection);
  119.  
  120. }
  121. else
  122. {
  123. // user has arrived at the page for the first time, just show them the form:
  124.  
  125. // show signin form:
  126. $show_signin_form = true;
  127. }
  128.  
  129. if ($show_signin_form)
  130. {
  131. // show the form that allows users to log in
  132. // Note we use an HTTP POST request to avoid their password appearing in the URL:
  133. echo <<<_END
  134. <form action="sign_in.php" method="post">
  135. Please enter your username and password:<br>
  136. Username: <input type="text" name="username" maxlength="16" value="$username" required> $username_val
  137. <br>
  138. Password: <input type="password" name="password" maxlength="16" value="$password" required> $password_val
  139. <br>
  140. <input type="submit" value="Submit">
  141. </form>
  142. _END;
  143. }
  144.  
  145. // display our message to the user:
  146. echo $message;
  147.  
  148. // finish off the HTML for this page:
  149. require_once "footer.php";
  150. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement