What are the best practices for updating and maintaining a list of blocked IPs in PHP?

When maintaining a list of blocked IPs in PHP, it is important to regularly update the list to include new IPs and remove old ones. One way to achieve this is by storing the list of blocked IPs in a database or a file and regularly checking against this list when processing incoming requests. Additionally, implementing a mechanism to automatically block IPs based on certain criteria can help in keeping the list up to date.

// Sample code to check if an IP is blocked
$blocked_ips = ['192.168.1.1', '10.0.0.1']; // List of blocked IPs

$ip_to_check = $_SERVER['REMOTE_ADDR']; // Get the IP of the current request

if (in_array($ip_to_check, $blocked_ips)) {
    // IP is blocked, handle accordingly (e.g., return a 403 Forbidden response)
    http_response_code(403);
    die("You are not allowed to access this resource.");
} else {
    // IP is not blocked, continue processing the request
    echo "Welcome!";
}