Skip to content
PHP Logo

How can PHP developers handle IP address tracking and blocking effectively in their scripts?

To handle IP address tracking and blocking effectively in PHP scripts, developers can utilize the $_SERVER['REMOTE_ADDR'] variable to retrieve the IP address of the client. They can then compare this IP address against a list of blocked IPs and take appropriate actions based on the result.

php
$blocked_ips = array("127.0.0.1", "192.168.0.1");

$client_ip = $_SERVER['REMOTE_ADDR'];

if (in_array($client_ip, $blocked_ips)) {
    // IP address is in the blocked list, take action (e.g., display an error message or redirect)
    echo "Access denied. Your IP address has been blocked.";
    exit;
} else {
    // IP address is not blocked, continue with the script
    echo "Welcome!";
}