How can error handling and reporting be improved in the provided PHP script to troubleshoot issues with IP blocking?

To improve error handling and reporting in the provided PHP script to troubleshoot issues with IP blocking, we can implement try-catch blocks to catch exceptions and log detailed error messages. Additionally, we can use functions like error_log() to record errors in a log file for further analysis.

try {
    // Your existing code for IP blocking goes here

    // If an exception is thrown, catch it and log the error message
} catch (Exception $e) {
    $error_message = "Error: " . $e->getMessage();
    error_log($error_message);
}