How does the HTTP protocol handle responses from the server to the client?
The HTTP protocol handles responses from the server to the client by sending a status code along with the response data. The status code indicates the success or failure of the request, with common codes like 200 for success, 404 for not found, and 500 for server errors. The response data can be in various formats such as HTML, JSON, or XML, depending on the request.
<?php
// Send a successful response with a status code of 200 and JSON data
header("Content-Type: application/json");
echo json_encode(["message" => "Success"]);
http_response_code(200);
?>
Related Questions
- In PHP, how does the lock function differ from trylock in terms of waiting for other threads to release locks?
- What are the potential security risks associated with using $_REQUEST instead of $_GET or $_POST in PHP?
- What are the common pitfalls to avoid when fetching and displaying data from multiple tables in PHP?