Can someone explain how to implement logic in PHP to store an IP address when a server is online and remove it when the server is offline?

To store an IP address when a server is online and remove it when the server is offline, you can create a simple PHP script that checks the server's status periodically. When the server is online, the script can store the IP address in a database or a file. When the server is offline, the script can remove the IP address from the storage.

// Check if the server is online
$server_ip = '192.168.1.1'; // Replace with your server's IP address
$server_status = @fsockopen($server_ip, 80, $errno, $errstr, 1);

if ($server_status) {
    // Server is online, store the IP address
    $ip_address = $_SERVER['REMOTE_ADDR'];
    
    // Store the IP address in a file
    file_put_contents('online_servers.txt', $ip_address . PHP_EOL, FILE_APPEND);
} else {
    // Server is offline, remove the IP address
    $ip_address = $_SERVER['REMOTE_ADDR'];
    
    // Read the stored IP addresses
    $stored_ips = file('online_servers.txt', FILE_IGNORE_NEW_LINES);
    
    // Remove the IP address from the file
    $key = array_search($ip_address, $stored_ips);
    if ($key !== false) {
        unset($stored_ips[$key]);
    }
    
    // Write back the updated IP addresses
    file_put_contents('online_servers.txt', implode(PHP_EOL, $stored_ips));
}