PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/views/dashboard/admin_sign_up.php

https://bitbucket.org/Lbkoger42/seedfork_php
PHP | 326 lines | 239 code | 45 blank | 42 comment | 32 complexity | 6787ce55d27ad7cac85079584cd529bd MD5 | raw file
  1. <?php
  2. // Include config file
  3. require_once 'config.php';
  4. // Define variables and initialize with empty values
  5. $username = $password = $confirm_password = $first_name = $last_name =
  6. $street = $city = $state = $zip_code = $phone = $email = "";
  7. $username_err = $password_err = $confirm_password_err = $first_name_err = $last_name_err =
  8. $street_err = $city_err = $state_err = $zip_code_err = $phone_err = $email_err = "";
  9. // Processing form data when form is submitted
  10. if($_SERVER["REQUEST_METHOD"] == "POST"){
  11. // Validate username
  12. if(empty(trim($_POST["username"]))){
  13. $username_err = "Please enter a username.";
  14. } else{
  15. // Prepare a select statement
  16. $sql = "SELECT id FROM users WHERE username = ?";
  17. if($stmt = mysqli_prepare($link, $sql)){
  18. // Bind variables to the prepared statement as parameters
  19. mysqli_stmt_bind_param($stmt, "s", $param_username);
  20. // Set parameters
  21. $param_username = trim($_POST["username"]);
  22. // Attempt to execute the prepared statement
  23. if(mysqli_stmt_execute($stmt)){
  24. /* store result */
  25. mysqli_stmt_store_result($stmt);
  26. if(mysqli_stmt_num_rows($stmt) == 1){
  27. $username_err = "This username is already taken.";
  28. } else{
  29. $username = trim($_POST["username"]);
  30. }
  31. } else{
  32. echo "Oops! Something went wrong. Please try again later.";
  33. }
  34. }
  35. // Close statement
  36. mysqli_stmt_close($stmt);
  37. }
  38. // Validate password
  39. if(empty(trim($_POST['password']))){
  40. $password_err = "Please enter a password.";
  41. } elseif(strlen(trim($_POST['password'])) < 6){
  42. $password_err = "Password must have atleast 6 characters.";
  43. } else{
  44. $password = trim($_POST['password']);
  45. }
  46. // Validate confirm password
  47. if(empty(trim($_POST["confirm_password"]))){
  48. $confirm_password_err = 'Please confirm password.';
  49. } else{
  50. $confirm_password = trim($_POST['confirm_password']);
  51. if($password != $confirm_password){
  52. $confirm_password_err = 'Password did not match.';
  53. }
  54. }
  55. // Validate first_name
  56. if(empty(trim($_POST['first_name']))){
  57. $first_name_err = "Please enter your first name.";
  58. } else{
  59. $first_name = trim($_POST['first_name']);
  60. }
  61. // Validate last_name
  62. if(empty(trim($_POST['last_name']))){
  63. $last_name_err = "Please enter your last name.";
  64. } else{
  65. $last_name = trim($_POST['last_name']);
  66. }
  67. // Validate organization
  68. /*
  69. if(empty(trim($_POST['organization']))){
  70. $organization_err = "Please enter your organization name.";
  71. } else{
  72. $organization = trim($_POST['organization']);
  73. }
  74. */
  75. // Validate street
  76. if(empty(trim($_POST['street']))){
  77. $street_err = "Please enter your street address.";
  78. } else{
  79. $street = trim($_POST['street']);
  80. }
  81. // Validate city
  82. if(empty(trim($_POST['city']))){
  83. $city_err = "Please enter a city.";
  84. } else{
  85. $city = trim($_POST['city']);
  86. }
  87. // Validate state
  88. if(empty(trim($_POST['state']))){
  89. $state_err = "Please enter a state.";
  90. } else{
  91. $state = trim($_POST['state']);
  92. }
  93. // Validate zip_code
  94. if(empty(trim($_POST['zip_code']))){
  95. $zip_code_err = "Please enter a zip code.";
  96. } else{
  97. $zip_code = trim($_POST['zip_code']);
  98. }
  99. // Validate phone
  100. if(empty(trim($_POST['phone']))){
  101. $phone_err = "Please enter a phone number.";
  102. } else{
  103. $phone = trim($_POST['phone']);
  104. }
  105. // Validate email
  106. if(empty(trim($_POST['email']))){
  107. $email_err = "Please enter an email.";
  108. } else{
  109. $email = trim($_POST['email']);
  110. }
  111. // Check input errors before inserting in database
  112. if(empty($username_err) && empty($password_err) && empty($confirm_password_err) && empty($first_name_err) && empty($last_name_err) && empty($street_err) && empty($city_err) && empty($state_err) && empty($zip_code_err) && empty($phone_err) && empty($email_err) ){
  113. // Prepare an insert statement
  114. $sql = "INSERT INTO users (username, password, role, first_name, last_name, street, city, state, zip_code, phone, email) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  115. if($stmt = mysqli_prepare($link, $sql)){
  116. // Bind variables to the prepared statement as parameters
  117. mysqli_stmt_bind_param($stmt, "sssssssssss", $param_username, $param_password, $param_role, $param_first_name, $param_last_name,
  118. $param_street, $param_city, $param_state, $param_zip_code, $param_phone, $param_email);
  119. // Set parameters
  120. $param_username = $username;
  121. $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  122. $param_role = 'Administrator';
  123. $param_first_name = $first_name;
  124. $param_last_name = $last_name;
  125. $param_street = $street;
  126. $param_city = $city;
  127. $param_state = $state;
  128. $param_zip_code = $zip_code;
  129. $param_phone = $phone;
  130. $param_email = $email;
  131. // Attempt to execute the prepared statement
  132. if(mysqli_stmt_execute($stmt)){
  133. // Redirect to login page
  134. header("location: login.php");
  135. } else{
  136. echo "Something went wrong. Please try again later.";
  137. }
  138. }
  139. // Close statement
  140. mysqli_stmt_close($stmt);
  141. }
  142. // Close connection
  143. mysqli_close($link);
  144. }
  145. ?>
  146. <html>
  147. <head>
  148. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  149. </head>
  150. <body>
  151. <!--Navigation bar-->
  152. <div id="nav-placeholder">
  153. </div>
  154. <script>
  155. $(function(){
  156. $("#nav-placeholder").load("application.php");
  157. });
  158. </script>
  159. <!--end of Navigation bar-->
  160. <!-- Logo -->
  161. <div class="row">
  162. <div class="background col-md-12">
  163. <div class="logo">
  164. <img src="/assets/images/seed.png" />
  165. </div>
  166. </div>
  167. </div>
  168. <div class="wrapper">
  169. <h2>Administrator Sign Up</h2>
  170. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  171. <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  172. <input placeholder="Username" type="text" name="username"class="form-control" value="<?php echo $username; ?>">
  173. <span class="help-block"><?php echo $username_err; ?></span>
  174. </div>
  175. <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  176. <input placeholder="Password" type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  177. <span class="help-block"><?php echo $password_err; ?></span>
  178. </div>
  179. <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  180. <input placeholder="Confirm Password" type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  181. <span class="help-block"><?php echo $confirm_password_err; ?></span>
  182. </div>
  183. <div class="form-group <?php echo (!empty($first_name_err)) ? 'has-error' : ''; ?>">
  184. <input placeholder="First" type="text" name="first_name"class="form-control" value="<?php echo $first_name; ?>">
  185. <span class="help-block"><?php echo $first_name_err; ?></span>
  186. </div>
  187. <div class="form-group <?php echo (!empty($last_name_err)) ? 'has-error' : ''; ?>">
  188. <input placeholder="Last" type="text" name="last_name"class="form-control" value="<?php echo $last_name; ?>">
  189. <span class="help-block"><?php echo $last_name_err; ?></span>
  190. </div>
  191. <!--
  192. <div class="form-group <?php echo (!empty($organization_err)) ? 'has-error' : ''; ?>">
  193. <input placeholder="Organization" type="text" name="organization"class="form-control" value="<?php echo $organization; ?>">
  194. <span class="help-block"><?php echo $organization_err; ?></span>
  195. </div>
  196. -->
  197. <div class="form-group <?php echo (!empty($street_err)) ? 'has-error' : ''; ?>">
  198. <input placeholder="Street" type="text" name="street"class="form-control" value="<?php echo $street; ?>">
  199. <span class="help-block"><?php echo $street_err; ?></span>
  200. </div>
  201. <div class="form-group <?php echo (!empty($city_err)) ? 'has-error' : ''; ?>">
  202. <input placeholder="City" type="text" name="city"class="form-control" value="<?php echo $city; ?>">
  203. <span class="help-block"><?php echo $city_err; ?></span>
  204. </div>
  205. <!-- state needs to be made into a dropdown window giving only 2 character options -->
  206. <div class="form-group <?php echo (!empty($state_err)) ? 'has-error' : ''; ?>">
  207. <input placeholder="State" type="text" name="state"class="form-control" value="<?php echo $state; ?>">
  208. <span class="help-block"><?php echo $state_err; ?></span>
  209. </div>
  210. <div class="form-group <?php echo (!empty($zip_code_err)) ? 'has-error' : ''; ?>">
  211. <input placeholder="Zip Code" type="text" name="zip_code"class="form-control" value="<?php echo $zip_code; ?>">
  212. <span class="help-block"><?php echo $zip_code_err; ?></span>
  213. </div>
  214. <div class="form-group <?php echo (!empty($phone_err)) ? 'has-error' : ''; ?>">
  215. <input placeholder="Phone" type="text" name="phone"class="form-control" value="<?php echo $phone; ?>">
  216. <span class="help-block"><?php echo $phone_err; ?></span>
  217. </div>
  218. <div class="form-group <?php echo (!empty($email_err)) ? 'has-error' : ''; ?>">
  219. <input placeholder="Email" type="text" name="email"class="form-control" value="<?php echo $email; ?>">
  220. <span class="help-block"><?php echo $email_err; ?></span>
  221. </div>
  222. <div class="form-group">
  223. <input type="submit" class="btn btn-warning" value="Submit">
  224. <input type="reset" class="btn btn-default" value="Reset">
  225. </div>
  226. <p>Already have an account? <a href="login.php">Login here</a>.</p>
  227. </form>
  228. </div>
  229. </body>
  230. <style type="text/css">
  231. .about-content {
  232. margin-top: 1em;
  233. }
  234. .wrapper{ width: 350px; padding: 20px; margin: 0 auto;}
  235. .background {
  236. background-size: cover;
  237. background-position: center center;
  238. position: relative;
  239. height: 8vw;
  240. min-height: 250px;
  241. width: 100%;
  242. }
  243. .logo {
  244. }
  245. .logo > img {
  246. min-width: 250px;
  247. width: 27%;
  248. left: 50%;
  249. position: absolute;
  250. transform: translateX(-50%);
  251. }
  252. #signuprow {
  253. }
  254. #user {
  255. padding-left: 25%;
  256. padding-right: 25%;
  257. padding-top: 10px;
  258. text-align: center;
  259. }
  260. #pass {
  261. padding-left: 25%;
  262. padding-right: 25%;
  263. padding-top: 5px;
  264. padding-bottom: 5px;
  265. text-align: center;
  266. }
  267. #confpass {
  268. padding-left: 25%;
  269. padding-right: 25%;
  270. padding-top: 0px;
  271. padding-bottom: 5px;
  272. text-align: center;
  273. }
  274. #signupbutt {
  275. padding-left:47%;
  276. }
  277. #signupheader {
  278. text-align: center;
  279. }
  280. </style>
  281. </html>