In this guide, we will explore how to expose a REST API within a PHP authentication system. Creating a secure and efficient REST API allows you to interact with your authentication features over the web.
<?php
header("Content-Type: application/json");
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// Get the posted data
$data = json_decode(file_get_contents("php://input"));
// Validate and authenticate user here
// (assume $username and $password were submitted in the POST request)
$username = $data->username;
$password = $data->password;
// Example user validation (replace with database check)
if ($username == "user" && $password == "pass") {
echo json_encode(["status" => "success", "message" => "Logged in successfully."]);
} else {
echo json_encode(["status" => "error", "message" => "Invalid credentials."]);
}
} else {
echo json_encode(["status" => "error", "message" => "Invalid request method."]);
}
?>
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?