Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RewriteEngine On # Turn on the rewriting engine
- RewriteRule ^users/?$ users.php [NC,L]
- RewriteRule ^users/([0-9]+)/?$ users.php?id=$1 [NC,L]
- <?php
- // Connect to database
- include("../connection.php");
- $db = new Database();
- $connection = $db->getConnstring();
- $request_method = $_SERVER["REQUEST_METHOD"];
- switch($request_method) {
- case 'GET':
- // Retrieve Users
- if (!empty($_GET["user_id"])) {
- $id = intval($_GET["user_id"]);
- get_users($user_id);
- } else {
- get_users();
- }
- break;
- case 'POST':
- // Create User
- create_user();
- break;
- default:
- // Invalid Request Method
- header("HTTP/1.0 405 Method Not Allowed");
- break;
- }
- function get_users($user_id=0) {
- global $connection;
- $query = "SELECT * FROM user";
- if ($user_id != 0) {
- $query .= "Where user_id=" . $user_id . "LIMIT 1";
- }
- $response = array();
- $result = mysqli_query($connection, $query);
- while ($row=mysqli_fetch_array($result)) {
- $response[] = $row;
- }
- header('Content-Type: application/json');
- echo json_encode($response);
- }
- function create_user() {
- global $connection;
- $data = json_decode(file_get_contents('php://input'), true);
- $username = $data["username"];
- $email = $data["email"];
- $password = $data["password"];
- $confirm_password = $data["confirmPassword"];
- $status = 0;
- $user_role = 0;
- echo $query = "INSERT INTO user SET username='" . $username . "', email='" . $email . "', password='" . $password . "', confirm_password='" . $confirm_password . "', status='" . $status . "', user_role='" . $user_role . "'";
- if (mysqli_query($connection, $query)) {
- $response = array(
- 'status' => 200,
- 'message' => 'User Registered Successfully.'
- );
- } else {
- $response = array(
- 'status' => 500,
- 'message' => 'User Addition Failedl'
- );
- }
- header('Content-Type: application/json');
- echo json_encode($response);
- }
- ?>
Add Comment
Please, Sign In to add comment