Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include "Functions.php";
- $filepath = "./user_data.dat";
- //$Master_user_array = array(
- //$user1 = array('username' => "john", 'password' => "doe", 'email' => 'johndoe@hawaii.edu'));
- //array_to_arrayfile($Master_user_array, $filepath);
- $Master_user_array = arrayfile_to_array($filepath);
- $usernameErr = $emailErr = $passwordErr = $cpasswordErr = "";
- $username = $email = $password = $cpassword ="";
- $no_errors = true;
- // checking if user input new items
- if(array_key_exists('create', $_POST)){
- $newname = $_POST['username'];
- if (empty($_POST["username"])) {
- $usernameErr = "Username is required";
- $no_errors = false;
- } else {
- $username = test_input($_POST["username"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z0-9 ]*$/",$username)) {
- $usernameErr = "Only letters, numbers and white space allowed";
- $no_errors = false;
- }
- }
- if (array_key_exists($newname, $Master_user_array)){
- $usernameErr = "Username already exists";
- $no_errors = false;
- }
- if(empty($_POST["password"])){
- $passwordErr = "Password is required";
- $no_errors = false;
- }
- if (strlen($_POST["password"]) <'6') {
- $passwordErr = "Your Password Must Contain At Least 6 Characters!";
- $no_errors = false;
- }
- else {
- $password = test_input($_POST["password"]);
- $cpassword = test_input($_POST["cpassword"]);
- }
- if(($_POST["password"] !== $_POST["cpassword"])) {
- $cpasswordErr = "Passwords do not match";
- $no_errors = false;
- }
- if (empty($_POST["email"])) {
- $emailErr = "Email is required";
- $no_errors = false;
- } else {
- $email = test_input($_POST["email"]);
- // check if e-mail address is well-formed
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $emailErr = "Invalid email format";
- $no_errors = false;
- }
- }
- if ($no_errors == true){
- $new_users = array('username' => $_POST['username'], 'password' => $_POST['password'], 'email' => $_POST['email']);
- $Master_user_array[strtolower($new_users['username'])] = $new_users;// convert to lower case, name of individual user data array in
- array_to_arrayfile($Master_user_array, $filepath);
- }
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <h2>Create A New User</h2>
- <p><span class="error">* required field.</span></p>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Username: <input type="text" name="username" value="<?php echo $username;?>">
- <span class="error">* <?php echo $usernameErr;?></span>
- <br><br>
- Password: <input type="password" name="password" value="<?php echo $password;?>">
- <span class="error">* <?php echo $passwordErr;?></span>
- <br><br>
- Confirm Password: <input type="password" name="cpassword" value="<?php echo $cpassword;?>">
- <span class="error">* <?php echo $cpasswordErr;?></span>
- <br><br>
- E-mail: <input type="text" name="email" value="<?php echo $email;?>">
- <span class="error">* <?php echo $emailErr;?></span>
- <br><br>
- <br><br>
- <input type="submit" name="create" value="Create">
- </form>
Add Comment
Please, Sign In to add comment