-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistration.php
137 lines (121 loc) · 5.21 KB
/
registration.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
session_start();
function get_contents($filename){
$raw_contents = file_get_contents($filename);
return json_decode($raw_contents, TRUE);
}
function put_contents($filename, $contents){
file_put_contents($filename, json_encode($contents));
}
function add_contents($filename, $new_content){
$contents = get_contents($filename);
$contents[] = $new_content;
put_contents($filename, $contents);
}
function filter_contents($filename, $predicate){
$contents = get_contents($filename);
return array_filter($contents, $predicate);
}
$registered_users = get_contents('registered.json') ? get_contents('registered.json') : [] ;
$errors = [];
function requiredCheck($val){
return empty($val);
}
function repeatCheck($registered_users, $email){
$flag = false;
foreach ($registered_users as $user){
if ($user['email'] === $email){
$flag = true;
break;
}
}
return $flag;
}
if ($_POST){
$name = $_POST['fullname'];
$ssn = $_POST['ssn'];
$address = $_POST['address'];
$email = $_POST['email'];
$pswd = $_POST['pswd'];
$rpt_pswd = $_POST['repeat-psw'];
if (requiredCheck($name)){
$errors[] = 'Name is required!';
}
if (requiredCheck($ssn)){
$errors[] = 'SSN is required!';
}else if (!is_numeric($ssn)){
$errors[] = 'SSN should be numeric!';
}else if (strlen($ssn) !== 9){
$errors[] = 'SSN should be a 9 digit number!';
}
if (requiredCheck($address)){
$errors[] = 'Address is required!';
}
if (requiredCheck($email)){
$errors[] = 'Email is required!';
}else if (!stristr($email, '')){
$errors[] = 'Email not formatted well!';
}
if (requiredCheck($pswd)){
$errors[] = 'Password is required!';
}
if (requiredCheck($rpt_pswd)){
$errors[] = 'Password Confirmation is required!';
}else if ($rpt_pswd !== $pswd){
$errors[] = 'Password Confirmation should match the Password!';
}else if (repeatCheck($registered_users, $email)){
$errors[] = 'Email already in use!';
}
if (empty($errors)){
$registered_users[] = [
'name' => $name,
'address' => $address,
'ssn' => $ssn,
'email' => $email,
'password' => $pswd
];
put_contents('registered.json', $registered_users);
header("Location:login.php");
exit;
}
}
function setValue($val, $errors) {
return $_POST && !empty($errors) ? $_POST[$val] : '';
}
?>
<link rel="stylesheet" href="default.css">
<form method="POST" novalidate>
<div class="container">
<h1>Register</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<label><b>Full Name</b></label>
<input type="text" name="fullname" value="<?= setValue('fullname',$errors)?>">
<p style="color:red"><?= $_POST ? requiredCheck($name)? 'Name is required!' : '' : ''?></p>
<label><b>SSN Number</b></label>
<input type="text" name="ssn" value="<?= setValue('ssn',$errors)?>">
<p style="color:red"><?= $_POST ? requiredCheck($ssn)? 'SSN is required!' : '' : ''?></p>
<p style="color:red"><?= $_POST && !requiredCheck($ssn) ? !is_numeric($ssn) ? 'SSN should be numeric!' : '': ''?></p>
<p style="color:red"><?= $_POST && !requiredCheck($ssn) && is_numeric($ssn) ? strlen($ssn) !=9 ? 'SSN should be a 9 digit number!' : '': ''?></p>
<label><b>Address</b></label>
<input type="text" name="address" value="<?= setValue('address',$errors)?>">
<p style="color:red"><?= $_POST ? requiredCheck($address)? 'Address is required!' : '' : ''?></p>
<label><b>Email</b></label>
<input type="text" name="email" value="<?= setValue('email',$errors)?>">
<p style="color:red"><?= $_POST ? requiredCheck($email)? 'Email is required!' : '' : ''?></p>
<p style="color:red"><?= $_POST && !requiredCheck($email)? !stristr($email, '@') ? 'Email not well formatted!': '' : ''?></p>
<p style="color:red"><?= $_POST && !requiredCheck($email) && stristr($email, '@') ? repeatCheck($registered_users,$email) ? 'Email already in use!': '' : ''?></p>
<label><b>Password</b></label>
<input type="password" name="pswd" value="<?= setValue('pswd',$errors)?>">
<p style="color:red"><?= $_POST ? requiredCheck($pswd)? 'Password is required!' : '' : ''?></p>
<label><b>Repeat Password</b></label>
<input type="password" name="repeat-psw" value="<?= setValue('repeat-psw', $errors)?>">
<p style="color:red"><?= $_POST ? requiredCheck($rpt_pswd)? 'Password Confirmation is required!' : '' : ''?></p>
<p style="color:red"><?= $_POST && !requiredCheck($rpt_pswd) ? $pswd != $rpt_pswd ? 'Password Confirmation should match the Password!': '' : ''?></p>
<hr>
<button type="submit" class="registerbtn">Register</button>
</div>
<div class="container signin">
<p>Already have an account? <a href="login.php">Login</a>.</p>
</div>
</form>