Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. <?php
  2.  
  3. // Things to notice:
  4. // The main job of this script is to execute an INSERT or UPDATE statement to create or update a user's profile information...
  5. // ... but only once the data the user supplied has been validated on the client-side, and then sanitised ("cleaned") and validated again on the server-side
  6. // It's your job to add these steps into the code
  7. // Both sign_up.php and sign_in.php do client-side validation, followed by sanitisation and validation again on the server-side -- you may find it helpful to look at how they work
  8. // HTML5 can validate all the profile data for you on the client-side
  9. // The PHP functions in helper.php will allow you to sanitise the data on the server-side and validate *some* of the fields...
  10. // ... but you'll also need to add some new PHP functions of your own to validate email addresses and dates
  11.  
  12. // execute the header script:
  13. require_once "header.php";
  14.  
  15. // default values we show in the form:
  16. $firstname = "";
  17. $lastname = "";
  18. $pets = "";
  19. $email = "";
  20. $dob = "";
  21. // strings to hold any validation error messages:
  22. $firstname_val = "";
  23. $lastname_val = "";
  24. $pets_val = "";
  25. $email_val = "";
  26. $dob_val = "";
  27. // should we show the set profile form?:
  28. $show_profile_form = false;
  29. // message to output to user:
  30. $message = "";
  31.  
  32. if (!isset($_SESSION['loggedInSkeleton']))
  33. {
  34. // user isn't logged in, display a message saying they must be:
  35. echo "You must be logged in to view this page.<br>";
  36. }
  37. elseif (isset($_POST['firstname']))
  38. {
  39. // user just tried to update their profile
  40.  
  41. // connect directly to our database (notice 4th argument) we need the connection for sanitisation:
  42. $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  43.  
  44. // if the connection fails, we need to know, so allow this exit:
  45. if (!$connection)
  46. {
  47. die("Connection failed: " . $mysqli_connect_error);
  48. }
  49.  
  50. // SANITISATION CODE MISSING:
  51.  
  52. // take copies of the credentials the user submitted and sanitise (clean) them:
  53. $firstname = sanitise ($_POST['firstname'], $connection);
  54. $lastname = sanitise ($_POST['lastname'], $connection);
  55. $pets = sanitise ($_POST['pets'], $connection);
  56. $email = sanitise ($_POST['email'], $connection);
  57. $dob = sanitise ($_POST['dob'], $connection);
  58.  
  59. // This is the code for server side validation, each field is validated seperately and has each have different conditions to be validated against
  60. $firstname_val = validateString($firstname, 5, 16);
  61. $lastname_val = validateString($lastname, 5, 16);
  62. $pets_val = validateInt($pets,0,128);
  63. if(!filter_var($email,FILTER_VALIDATE_EMAIL))
  64. {
  65. $email_val="$email is not valid";
  66. }
  67. //$dob_val = validateDOB($dob);
  68.  
  69.  
  70. //any errors will be stored in these variables
  71. $errors = $firstname_val . $lastname_val . $pets_val . $email_val;
  72.  
  73.  
  74. // check that all the validation tests passed before going to the database:
  75. if ($errors == "")
  76. {
  77. // read their username from the session:
  78. $username = $_SESSION["username"];
  79.  
  80. // now write the new data to our database table...
  81.  
  82. // check to see if this user already had a favourite:
  83. $query = "SELECT * FROM profiles WHERE username='$username'";
  84.  
  85. // this query can return data ($result is an identifier):
  86. $result = mysqli_query($connection, $query);
  87.  
  88. // how many rows came back? (can only be 1 or 0 because username is the primary key in our table):
  89. $n = mysqli_num_rows($result);
  90.  
  91. // if there was a match then UPDATE their profile data, otherwise INSERT it:
  92. if ($n > 0)
  93. {
  94. // we need an UPDATE:
  95. $query = "UPDATE profiles SET firstname='$firstname',lastname='$lastname',pets=$pets,email='$email',dob='$dob' WHERE username='$username'";
  96. $result = mysqli_query($connection, $query);
  97. }
  98. else
  99. {
  100. // we need an INSERT:
  101. $query = "INSERT INTO profiles (username,firstname,lastname,pets,email,dob) VALUES ('$username','$firstname','$lastname',$pets,'$email','$dob')";
  102. $result = mysqli_query($connection, $query);
  103. }
  104.  
  105. // no data returned, we just test for true(success)/false(failure):
  106. if ($result)
  107. {
  108. // show a successful update message:
  109. $message = "Profile successfully updated<br>";
  110. }
  111. else
  112. {
  113. // show the set profile form:
  114. $show_profile_form = true;
  115. // show an unsuccessful update message:
  116. $message = "Update failed<br>";
  117. }
  118. }
  119. else
  120. {
  121. // validation failed, show the form again with guidance:
  122. $show_profile_form = true;
  123. // show an unsuccessful update message:
  124. $message = "Update failed, please check the errors above and try again<br>";
  125. }
  126.  
  127. // we're finished with the database, close the connection:
  128. mysqli_close($connection);
  129.  
  130. }
  131. else
  132. {
  133. // arrived at the page for the first time, show any data already in the table:
  134.  
  135. // read the username from the session:
  136. $username = $_SESSION["username"];
  137.  
  138. // now read their profile data from the table...
  139.  
  140. // connect directly to our database (notice 4th argument):
  141. $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  142.  
  143. // if the connection fails, we need to know, so allow this exit:
  144. if (!$connection)
  145. {
  146. die("Connection failed: " . $mysqli_connect_error);
  147. }
  148.  
  149. // check for a row in our profiles table with a matching username:
  150. $query = "SELECT * FROM profiles WHERE username='$username'";
  151.  
  152. // this query can return data ($result is an identifier):
  153. $result = mysqli_query($connection, $query);
  154.  
  155. // how many rows came back? (can only be 1 or 0 because username is the primary key in our table):
  156. $n = mysqli_num_rows($result);
  157.  
  158. // if there was a match then extract their profile data:
  159. if ($n > 0)
  160. {
  161. // use the identifier to fetch one row as an associative array (elements named after columns):
  162. $row = mysqli_fetch_assoc($result);
  163. // extract their profile data for use in the HTML:
  164. $firstname = $row['firstname'];
  165. $lastname = $row['lastname'];
  166. $pets = $row['pets'];
  167. $email = $row['email'];
  168. $dob = $row['dob'];
  169. }
  170.  
  171. // show the set profile form:
  172. $show_profile_form = true;
  173.  
  174. // we're finished with the database, close the connection:
  175. mysqli_close($connection);
  176.  
  177. }
  178.  
  179. if ($show_profile_form)
  180. {
  181. if(isset($_get['noval']))
  182. {
  183. // a version without client side validation
  184. echo <<<_END
  185. <form action="set_profile.php?noval=y" method="post">
  186. Update your profile info:<br>
  187. First name: <input type="text" name="firstname" value="$firstname"> $firstname_val
  188. <br>
  189. Last name: <input type="text" name="lastname" value="$lastname"> $lastname_val
  190. <br>
  191. Number of pets:<input type="text" name="pets" value="$pets"> $pets_val
  192. <br>
  193. Email address: <input type="text" name="email" value="$email"> $email_val
  194. <br>
  195. Date of birth: <input type="text" name="dob" value="$dob">
  196. <span class="error"</span>
  197. <br>
  198. <input type="submit" value="Submit">
  199. </form
  200. _END;
  201. }
  202. else
  203. {
  204. //client side validation. This block shows the max values for the fields again and I have also wrote required for each field which means that no field can be left blank
  205. echo <<<_END
  206. <form action="set_profile.php" method="post">
  207. Update your profile info:<br>
  208. First name: <input type="text" maxlength = "16" name="firstname" value="$firstname"required> $firstname_val
  209. <br>
  210. Last name: <input type="text" maxlength = "16" name="lastname" value="$lastname"required> $lastname_val
  211. <br>
  212. Number of pets:<input type="number" min="1" max="128"name="pets" value="$pets"required> $pets_val
  213. <br>
  214. Email address: <input type="email" maxlength = "50" name="email" value="$email"required> $email_val
  215. <br>
  216. Date of birth: <input type="date" name="dob" value="$dob"required>
  217. <span class="error"</span>
  218. <br>
  219. <input type="submit" value="Submit">
  220. </form>
  221. _END;
  222. }
  223. }
  224.  
  225.  
  226. // display our message to the user:
  227. echo $message;
  228.  
  229. // finish of the HTML for this page:
  230. require_once "footer.php";
  231. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement