Guest User

Untitled

a guest
Nov 3rd, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. <?php
  2. include "Functions.php";
  3. $filepath = "./user_data.dat";
  4. //$Master_user_array = array(
  5. //$user1 = array('username' => "john", 'password' => "doe", 'email' => 'johndoe@hawaii.edu'));
  6. //array_to_arrayfile($Master_user_array, $filepath);
  7.  
  8. $Master_user_array = arrayfile_to_array($filepath);
  9.  
  10. $usernameErr = $emailErr = $passwordErr = $cpasswordErr = "";
  11. $username = $email = $password = $cpassword ="";
  12.  
  13. $no_errors = true;
  14. // checking if user input new items
  15. if(array_key_exists('create', $_POST)){
  16. $newname = $_POST['username'];
  17.  
  18. if (empty($_POST["username"])) {
  19. $usernameErr = "Username is required";
  20. $no_errors = false;
  21. } else {
  22. $username = test_input($_POST["username"]);
  23. // check if name only contains letters and whitespace
  24. if (!preg_match("/^[a-zA-Z0-9 ]*$/",$username)) {
  25. $usernameErr = "Only letters, numbers and white space allowed";
  26. $no_errors = false;
  27. }
  28. }
  29. if (array_key_exists($newname, $Master_user_array)){
  30. $usernameErr = "Username already exists";
  31. $no_errors = false;
  32. }
  33. if(empty($_POST["password"])){
  34. $passwordErr = "Password is required";
  35. $no_errors = false;
  36. }
  37. if (strlen($_POST["password"]) <'6') {
  38. $passwordErr = "Your Password Must Contain At Least 6 Characters!";
  39. $no_errors = false;
  40. }
  41. else {
  42. $password = test_input($_POST["password"]);
  43. $cpassword = test_input($_POST["cpassword"]);
  44. }
  45. if(($_POST["password"] !== $_POST["cpassword"])) {
  46. $cpasswordErr = "Passwords do not match";
  47. $no_errors = false;
  48. }
  49.  
  50.  
  51. if (empty($_POST["email"])) {
  52. $emailErr = "Email is required";
  53. $no_errors = false;
  54. } else {
  55. $email = test_input($_POST["email"]);
  56. // check if e-mail address is well-formed
  57. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  58. $emailErr = "Invalid email format";
  59. $no_errors = false;
  60. }
  61. }
  62. if ($no_errors == true){
  63. $new_users = array('username' => $_POST['username'], 'password' => $_POST['password'], 'email' => $_POST['email']);
  64. $Master_user_array[strtolower($new_users['username'])] = $new_users;// convert to lower case, name of individual user data array in
  65. array_to_arrayfile($Master_user_array, $filepath);
  66.  
  67.  
  68. }
  69. }
  70.  
  71.  
  72.  
  73.  
  74. function test_input($data) {
  75. $data = trim($data);
  76. $data = stripslashes($data);
  77. $data = htmlspecialchars($data);
  78. return $data;
  79. }
  80. ?>
  81.  
  82. <h2>Create A New User</h2>
  83. <p><span class="error">* required field.</span></p>
  84. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  85. Username: <input type="text" name="username" value="<?php echo $username;?>">
  86. <span class="error">* <?php echo $usernameErr;?></span>
  87. <br><br>
  88. Password: <input type="password" name="password" value="<?php echo $password;?>">
  89. <span class="error">* <?php echo $passwordErr;?></span>
  90. <br><br>
  91. Confirm Password: <input type="password" name="cpassword" value="<?php echo $cpassword;?>">
  92. <span class="error">* <?php echo $cpasswordErr;?></span>
  93. <br><br>
  94. E-mail: <input type="text" name="email" value="<?php echo $email;?>">
  95. <span class="error">* <?php echo $emailErr;?></span>
  96. <br><br>
  97.  
  98. <br><br>
  99. <input type="submit" name="create" value="Create">
  100. </form>
Add Comment
Please, Sign In to add comment