-
Notifications
You must be signed in to change notification settings - Fork 0
/
processing.php
54 lines (45 loc) · 1.51 KB
/
processing.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
<?php
// 1. Empty fields
// 2. Existent username
// Username: test123
// 3. Password error
// Password is less than 6 characters
// 4. Other dump file that is not JSON
// echo 'not json';
$data = array();
$data['username'] = '';
if(isset($_POST['username'])) {
$data['username'] = trim($_POST['username']);
}
$data['password'] = '';
if(isset($_POST['password'])) {
$data['password'] = $_POST['password'];
}
$mandatory_fields = array("username","password");
$valid = true;
$result['result']['errors'] = array();
$result['result']['success'] = array();
// Check is email already exists
if(isset($data['username']) AND $data['username'] == 'test123'){
http_response_code(403);
array_push($result['result']['errors'],array("path" => array("username"),"message" => "Username is already taken"));
$valid=false;
}
if(isset($data['password']) AND strlen($data['password']) <= 6){
http_response_code(403);
array_push($result['result']['errors'],array("path" => array("password"),"message" => "Your password must contain at least 6 characters"));
$valid=false;
}
foreach ($mandatory_fields as $field) {
if (!isset($data[$field]) or $data[$field]=='') {
http_response_code(403);
array_push($result['result']['errors'],array("path" => array($field),"message" => "This field is mandatory"));
$valid=false;
}
}
if($valid) {
array_push($result['result']['success'],array("message" => "User Registered"));
http_response_code(200);
}
echo json_encode($result['result']);
?>