-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.php
executable file
·111 lines (94 loc) · 3.97 KB
/
register.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
require_once 'utils/functions.php';
require_once 'classes/User.php';
require_once 'classes/DB.php';
require_once 'classes/UserTable.php';
start_session();
// try to register the user - if there are any error/
// exception, catch it and send the user back to the
// login form with an error message
try {
$formdata = array();
$errors = array();
$input_method = INPUT_POST;
$formdata['username'] = filter_input($input_method, "username", FILTER_SANITIZE_STRING);
$formdata['job'] = filter_input($input_method, "job", FILTER_SANITIZE_STRING);
$formdata['password'] = filter_input($input_method, "password", FILTER_SANITIZE_STRING);
$formdata['cpassword'] = filter_input($input_method, "cpassword", FILTER_SANITIZE_STRING);
// throw an exception if any of the form fields
// are empty
if (empty($formdata['username'])) {
$errors['username'] = "Username required";
}
//$email = filter_var($formdata['username'], FILTER_VALIDATE_EMAIL);
//if ($email != $formdata['username']) {
// $errors['username'] = "Valid email required required";
//}
//checks if a valid opening hours is entered and inputs error if not valid
$job = array("hr", "Manager", "Admin", "Branch staff");
if ($formdata['job'] === Null || $formdata['job'] === False || $formdata['job'] === "") {
$errors['job'] = "Please enter a job type";
} elseif (!in_array($formdata['job'], $job)) {
$errors['job'] = "Please job type from the provided list";
}
if (empty($_POST['password'])) {
$errors['password'] = "Password required";
}
if (empty($formdata['cpassword'])) {
$errors['cpassword'] = "Confirm password required";
}
// if the password fields do not match
// then throw an exception
if (!empty($formdata['password']) && !empty($formdata['cpassword'])
&& $formdata['password'] != $formdata['cpassword']) {
$errors['password'] = "Passwords must match";
}
if (empty($errors)) {
// since none of the form fields were empty,
// store the form data in variables
$username = $formdata['username'];
$jobtpye = $formdata['job'];
$password = $formdata['password'];
$cpassword = $formdata['cpassword'];
// create a UserTable object and use it to retrieve
// the users
// $connection = DB::getConnection();
$connection = Connection::getInstance();
$userTable = new UserTable($connection);
$user = $userTable->getUserByUsername($username);
// since password fields match, see if the username
// has already been registered - if it is then throw
// and exception
if ($user != null) {
$errors['username'] = "Username already registered";
}
}
if (!empty($errors)) {
throw new Exception("There were errors. Please fix them.");
}
// since the username is not aleady registered, create
// a new User object, add it to the database using the
// UserTable object, and store it in the session array
// using the key 'user'
$user = new User(-1, $username, $password, $jobtpye);
$id = $userTable->insert($user);
$user->setId($id);
$_SESSION['newStaff'] = $user;
// now the user is registered and logged in so redirect
// them the their home page
// Note the user is redirected to home.php rather than
// requiring the home.php script at this point - this
// ensures that if the user refreshes the home page they
// will not be resubmitting the login form.
//
// require 'home.php';
header('Location: staffAdded.php');
}
catch (Exception $ex) {
// if an exception occurs then extract the message
// from the exception and send the user the
// registration form
$errorMessage = $ex->getMessage();
require 'newStaff.php';
}
?>