What are common pitfalls when using a counter script with IP blocking in PHP?

Common pitfalls when using a counter script with IP blocking in PHP include not properly sanitizing user input, not handling IP addresses correctly, and not securely storing the data. To solve these issues, make sure to validate and sanitize all user input, use proper functions to handle IP addresses, and securely store the IP addresses in a database.

// Validate and sanitize user input
$ip_address = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP);

// Use proper functions to handle IP addresses
$ip_blocked = false;
$blocked_ips = ['127.0.0.1', '192.168.0.1']; // Example of blocked IPs

if (in_array($ip_address, $blocked_ips)) {
    $ip_blocked = true;
}

// Securely store the IP addresses in a database
// Use prepared statements to prevent SQL injection
$stmt = $pdo->prepare("INSERT INTO blocked_ips (ip_address) VALUES (:ip_address)");
$stmt->bindParam(':ip_address', $ip_address);
$stmt->execute();