What are the common pitfalls to avoid when implementing a visitor counter with IP address filtering in PHP?

One common pitfall to avoid when implementing a visitor counter with IP address filtering in PHP is not properly sanitizing and validating user input. This can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To prevent this, always sanitize and validate user input before using it in your code.

// Sanitize and validate user input for IP address filtering
$ip_address = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP);

// Implement IP address filtering
$allowed_ips = array('127.0.0.1', '192.168.1.1'); // Define array of allowed IP addresses

if (in_array($ip_address, $allowed_ips)) {
    // Increment visitor counter
    $counter = file_get_contents('counter.txt');
    $counter++;
    file_put_contents('counter.txt', $counter);
}