What is the best practice for handling HTTP authorized requests in PHP?

When handling HTTP authorized requests in PHP, it is best practice to check for the presence of the Authorization header in the incoming request. If the header is present, you can extract the credentials (such as a token or username/password) and verify them against your authentication system. Once the credentials are validated, you can proceed with processing the request.

if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
    $authHeader = $_SERVER['HTTP_AUTHORIZATION'];
    // Extract and validate credentials from $authHeader
    // Proceed with processing the request
} else {
    // Handle unauthorized request
    http_response_code(401);
    echo "Unauthorized";
    exit();
}