-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignup.php
39 lines (39 loc) · 1.25 KB
/
signup.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
<?php
// Get the username and password from the POST request
$username = $_POST["username"];
$password = $_POST["password"];
// Check if the username and password are not empty
if ($username && $password) {
// Read the users.json file and decode it into an array
$file = file_get_contents("users.json");
$data = json_decode($file, true);
// Check if there is already a user with that username in the data array
$found = false;
for ($i = 0; $i < count($data); $i++) {
if ($data[$i]["username"] == $username) {
$found = true;
break;
}
}
if ($found) {
// Return an error response
echo "error";
} else {
// Create a new array with the username and password properties
$user = array(
"username" => $username,
"password" => $password
);
// Append the new array to the data array
array_push($data, $user);
// Encode the data array back into JSON format and write it to the users.json file
$file = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents("users.json", $file);
// Return a success response
echo "success";
}
} else {
// Return an error response
echo "error";
}
?>