What are common pitfalls when implementing an IP ban in PHP scripts?

Common pitfalls when implementing an IP ban in PHP scripts include not properly sanitizing user input, not handling IPv6 addresses correctly, and not considering the use of proxies or VPNs to bypass the ban. To address these issues, ensure that user input is sanitized and validated before applying the ban, use proper functions to handle IPv6 addresses, and consider implementing additional measures to detect and block proxy or VPN traffic.

// Example of implementing an IP ban with proper input sanitization and handling of IPv6 addresses

$ip = $_SERVER['REMOTE_ADDR'];

// Sanitize and validate the IP address
if (filter_var($ip, FILTER_VALIDATE_IP)) {
    // Check if the IP address is banned
    $banned_ips = ['127.0.0.1', '192.168.0.1']; // Example banned IPs
    if (in_array($ip, $banned_ips)) {
        // IP is banned, display an error message or redirect
        echo "Access denied.";
        exit;
    } else {
        // IP is not banned, continue with the script
        echo "Welcome!";
    }
} else {
    // Invalid IP address, handle error
    echo "Invalid IP address.";
}